1 / 12

Function

Function. Dong- Chul Kim BioMeCIS CSE @ UTA. Functions. A function is a self-contained unit of program code designed to accomplish a particular task . Some functions can cause an action (printf, scanf), and others can return a value for your program to use (strlen, strcmp).

shubha
Télécharger la présentation

Function

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. Function Dong-Chul Kim BioMeCIS 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 (printf, scanf), and others can return a value for your program to use (strlen, strcmp). • Why use functions? • Makes reuse of code easier • Reduces number of places to update • Hide implementation, letting programmer focus on functionality – more modular

  3. Example • 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; }

  4. Outline • 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?

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

  6. Example #include <stdio.h> 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"); }

  7. Function Declarations • 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

  8. Function Definitions • When defining our own functions, the general form is return_typefunction_name(input_typevariable_name) { /* something happens here */ } • Example: intaddnumbers(intnumber1, intnumber2) { intsum = number1 + number2; return sum; }

  9. The variable names in the function definition do not need to match the names in the function call, but the quantity 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. #include <stdio.h> int f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); return f2(y); } int f2(int y) { y = y + 1; printf("In f2, y = %d\n", y); return y; } int main(void) { int x = 15; f1(x); return 0; }

  11. Return and Input Types • 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); }

  12. Variable Scope • 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.

More Related