Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/2015 1.

Similar presentations


Presentation on theme: "FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/2015 1."— Presentation transcript:

1 FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1

2 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 Functions

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

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

5 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 Three terms

6 #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"); } Example

7 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 Declarations

8 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; } Function Definitions

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

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


Download ppt "FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/2015 1."

Similar presentations


Ads by Google