1 / 19

Functions

Functions. Dr. Sajib Datta CSE@UTA Sep 22 , 2014. 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

Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Dr. SajibDatta CSE@UTA Sep 22, 2014

  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 <stdio.h> • 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 <stdio.h> • void printstars(int num); /*Function prototype*/ • int main(void) • { • intnumstar; • 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 */ • { • inti; • for (i=0; i< num; i++) • { • printf("*"); • } • printf("\n"); • }

  8. Function definitions • When defining our own functions, the general form is • return_typefunction_name(input_typevariable_name) • { • /* something happens here */ • } • Example: • intaddnumbers(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 <stdio.h> • 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. Order of function definition • Wrong! Warning or error • #include <stdio.h> • intmain(void) • { • int x = 15; • f1(x); • return 0; • } • void f1(int y) • { • y = y + 1; • printf("In f1, y = %d\n", y); • }

  15. Proper guideline • 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

  16. Writing definition before main() • 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 <stdio.h> • void f1(int y) • { • y = y + 1; • printf("In f1, y = %d\n", y); • } • intmain(void) • { • int x = 15; • f1(x); • return 0; • }

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

  18. //Compare two integers • #include <stdio.h> • intLargerInt(int a, int b); • intmain(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; • } • intLargerInt(int a, int b) • { • if(a > b) • return 1; • else • return 0; • }

  19. Function and array • #include <stdio.h> • #define SIZE 10 • int sum(int ar[], int n); • intmain(void) • { • intmyarr[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) • { • inti, total = 0; • for(i = 0; i<n; i++) • total += ar[i]; • return total; • }

More Related