120 likes | 218 Vues
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).
E N D
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). • Why use functions? • Makes reuse of code easier • Reduces number of places to update • Hide implementation, letting programmer focus on functionality – more modular
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; }
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?
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
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"); }
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
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; }
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.
#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; }
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); }
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.