Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.

Similar presentations


Presentation on theme: "Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions."— Presentation transcript:

1 Functions Dr. Sajib Datta CSE@UTA

2 Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions can cause an action ▫Such as printf() and scanf() and others can return a value for your program to use ▫Such as strlen() and strcmp()

3 Why use functions Make reuse of code easier Reduce number of places to update Hide implementation, letting programmer focus on functionality

4 Why use functions cont. You want to write a program that does the following: Read in a list of numbers Sort the numbers Find their average Print out their average ▫#include ▫int main(void) ▫{ ▫ int list[10]; ▫ readlist(list, 10); ▫ sortlist(list, 10); ▫ average(list, 10); ▫ printavg(list, 10); ▫ return 0; ▫}

5 What do you need to know about functions? How to define functions properly? How to call them up for use? How to set up communication between functions?

6 Three terms A function definition ▫Specifies exactly what the function does A function call ▫Causes the function to be executed Function prototype ▫Tells the compiler what sort of function your_function() is

7 An example of a function #include void printstars(int num); /*Function prototype*/ int main(void) { int numstar; do { printf("Please input an integer (>0):"); scanf("%d", &numstar); }while(numstar <= 0); printstars(numstar);/*A function call*/ return 0; } void printstars(int num) /*A function definition */ { int i; for (i=0; i< num; i++) { printf("*"); } printf("\n"); }

8 Function definitions When defining our own functions, the general form is ▫return_type function_name(input_type variable_name) ▫{ ▫/* something happens here */ ▫} Example: ▫int addnumbers(int number1, int number2) ▫{ ▫int sum = number1 + number2; ▫return sum; ▫}

9 Function definitions cont. The names of the formal parameters in the function definition do not need to match the names in the function call, but the number and types should match. To return a value, we use the return keyword. We can declare variables in our function just as we did in main. We can call other functions from within our function.

10 Function definitions cont. The types of variables that we can pass or receive from a function can be any of the types that we declare variables to be–int, float, array (actually, we pass the address of the array), etc. What type do we use if we are not passing or not returning anything? Void Example: ▫void print2numbers(int number1, int number2) ▫{ ▫printf("%d + %d is %d\n", number1, number2, ▫number1 + number2); ▫}

11 Function definitions cont. When a function is called, an execution environment is created for the function and control passes to its code. The execution environment contains memory for the parameters to the function and any variables defined in the function. When the function terminates execution, the memory occupied by the function will be returned to the system for other use. So the parameters stored there will not be guaranteed to retain the contents.

12 Function definitions cont. We need to know the following when using variables in functions: The process used in this lecture for providing variable values to our function is called pass by value. When doing so, a copy of the variable is provided. Variables declared outside the function are unknown to the function unless we pass them. Variables declared within a function block are known only to that function.

13 Passing by value in function calls #include int f1(int y); int main(void) { int x = 15; printf("In main, before calling f1(x), x is %d.\n", x); f1(x); printf("In main, after calling f1(x), x is %d.\n", x); return 0; } int f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); return y; }

14 Wrong! Warning or error #include int main(void) { int x = 15; f1(x); return 0; } void f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); } Order of function definition

15 We must let the compiler know about the function prior to using it by either: Placing the function definition before main Placing a function declaration (or prototype) before main and define the function later Proper guideline

16 You have to define a function before you use it. Or you can declare the function before you use it and give the definition later. #include void f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); } int main(void) { int x = 15; f1(x); return 0; } Writing definition before main()

17 #include void f1(int y); int main(void) { int x = 15; f1(x); return 0; } void f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); } Writing function prototype/declaration before main()

18 //Compare two integers #include int LargerInt(int a, int b); int main(void) { int x, y; printf("Please input two integers:"); scanf("%d", &x); scanf("%d", &y); if(LargerInt(x,y)) { printf("a is larger than b.\n"); } else { printf("a is not larger than b.\n"); } return 0; } int LargerInt(int a, int b) { if(a > b) return 1; else return 0; }

19 #include #define SIZE 10 int sum(int ar[], int n); int main(void) { int myarr[SIZE] = {20, 10, 5, 39, 4, 16, 19, 26, 31, 20}, answer; answer = sum(myarr, SIZE); printf("The total sum of the integers in myarr is %d.\n", answer); printf("The size of myarr is %d bytes.\n", sizeof(myarr)); return 0; } int sum(int ar[], int n) { int i, total = 0; for(i = 0; i<n; i++) total += ar[i]; return total; } Function and array


Download ppt "Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions."

Similar presentations


Ads by Google