Functions Examples CSCI N305

Slides:



Advertisements
Similar presentations
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Advertisements

Introduction to Computer Programming in c
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Computer programming Lectures 6&7. Lectures 6&7: Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
User defined functions
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
User-Written Functions
- Standard C Statements
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Functions Examples CSCI 230
Arrays Declarations CSCI N305
Functions Dr. Sajib Datta
CSC113: Computer Programming (Theory = 03, Lab = 01)
Building Java Programs
Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Functions I Creating a programming with small logical units of code.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions Recursion CSCI 230
Loops in C.
Functions, Part 2 of 3 Topics Functions That Return a Value
Pointers Call-by-Reference CSCI 230
A function with one argument
Function In this lesson, you will learn about Introduction to Function
Computer programming Lecture 3.
Functions, Part 1 of 3 Topics Using Predefined Functions
CPS125 Week
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
Department of Statistics St. Joseph’s College (Autonomous)
Introduction to Computing Lecture 08: Functions (Part I)
Dale Roberts, Lecturer IUPUI
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Top-Down Design with Functions
Presentation transcript:

Functions Examples CSCI N305 Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Functions Examples

Simple function in C Writing a function does nothing unless it’s called Calling a function transfers flow of control into the function. When the function returns, flow of control returns back to the caller. #include <stdio.h> void print_message(void) { printf(“CSCI 230 is fun\n”); } int main(void) print_message(); return 0; Output: CSCI 230 is fun

Repeatedly calling functions Now that we have functionality isolated in a function, the function can be reused over and over again. #include <stdio.h> void print_message(void) { printf(“CSCI 230 is fun\n”); } int main(void) print_message(); return 0; Output: CSCI 230 is fun

Calling functions from with other control structures The function can be called from within other control structures. #include <stdio.h> void print_message(void) { printf(“CSCI 230 is fun\n”); } int main(void) int i; for (i = 1; i <= 5; i++) print_message(); return 0; Output: CSCI 230 is fun

Functions with input parameters Parameters are by default input parameters and passed by value. Output: Triangular number 10 is 55 Triangular number 20 is 210 Triangular number 50 is 1275 #include <stdio.h> void calculate_triangular_number(int n) { int i, triangular_number = 0; for (i = 1; i <= n; i++) triangular_number+=i; printf(“Triangular number %d is %d\n”, n, triangular_number); } int main(void) calculate_triangular_number(10); calculate_triangular_number(20); calculate_triangular_number(50); return 0; Calculating a triangular number, that is summing integers 1 to n, is very common in Computer Science. It is easily solved using summations. Do you know how to calculate the sum using a formula instead of looping?

Functions can return a single result Functions can return a single result through the return value. Just specify a return type on the function declaration, and be certain the return a value inside the function. Now, you can call the function inside an expression. #include <stdio.h> int calculate_triangular_number(int n) { int i, triangular_number = 0; for (i = 1; i <= n; i++) triangular_number+=i; return triangular_number; } int main(void) printf(“Triangular number %d is %d\n”, 10, calculate_triangular_number(10)); printf(“Triangular number %d is %d\n”, 20, calculate_triangular_number(20)); printf(“Triangular number %d is %d\n”, 50, calculate_triangular_number(50)); return 0; Output: Triangular number 10 is 55 Triangular number 20 is 210 Triangular number 50 is 1275

More Examples: Example 1: … Old style float mySquare(float); main() { float y; printf(“%f\n”, mySquare(5.0)); } float mySquare(float x) return x*x; float mySquare(x) float x; { return x*x; } Old style Example 2: /* calculate the sum from 1+2+…+n */ int sum(int n) { int i, sum = 0; for (i=1; i<=n; i++) sum += i; return(sum); } main() int j,n; printf(“input value n: “); scanf(“%d”,&n); for (j=1; j<=n; j++) printf(“sum from 1 to %d is %d\n”,j,sum(j));

Functional Decomposition Top-Down Programming, also called functional decomposition, uses functions to hide details of an algorithm inside the function. In the prior example, main called calculate_triangular_number without regards to how the function is implemented. You can write a call to calculate_triangular_number without concerning yourself at that time with the details of operation of that function. All you need to know is that you can develop the function at a later time. The same programming technique that makes programs easier to write also makes programs easier to read. The reader of main can easily see that is it calculating and displaying three triangular numbers. The reader need to sift through all the details of how a triangular number is actually calculated. [Kochan 137]

Acknowledgements Some function examples were obtained from Kochan, Programming in C.