Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.

Similar presentations


Presentation on theme: "Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions."— Presentation transcript:

1 Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions. The return statement Issues of program termination Functions that are recursive Chapter 9 - Functions

2 Our programs so far have consisted of just one function main. This chapter focuses on writing our own functions. A function is a series of statements that have been grouped together and given a name. Functions are the building blocks of C programs. Each function is essentially a small program, with its own declarations and statements. Using functions, we can divide a program into small pieces that are easier to follow, understand and modify. Functions are reusable: we can take a function that was originally part of one program and use it in others. Functions allow us to avoid duplicating code.

3 Function Definitions return-type function-name (parameters) { declarations statements } The "return type" of a function is the type of value that the function returns The following rules govern the return type: Functions may not return arrays, but no other restrictions on the return type. If the return type is omitted, the function is presumed to return a value of type int. Specifying the return type void indicates the function does not return a value. float average(float a, float b) { return (a + b) / 2; }

4 Declare each function before calling it. A function declaration provides the compiler with a brief glimpse at a function whose full definition will appear later. A function declaration is the first line of a function definition with a semicolon added at the end: return-type function-name (parameters); Function Declaration: #include float average(float a, float b); /* DECLARATION */ main() { float x, y, z; printf (“Enter three numbers: “); scanf("%f%f%f", &x, &y, &z) ; printf("Average of %g and %g: %g\n", x, y, average(x, y) ) ; printf("Average of %g and %g: %g\n", y, z, average(y, z) ) ; printf("Average of %g and %g: %g\n", x, z, average(x, z)) ; return 0 ; } float average(float a, float b)/* DEFINITION */ { return (a + b) / 2 ; }

5 float is average's return type: the type of data that the function returns each time it's called. The identifiers a and b (the function's parameters) represent the two numbers that will be supplied when average is called. A function parameter is essentially a variable whose initial value will be supplied later, when the function is called. Each parameter must have a type: float is the type of a and b. NOTE: (float must appear twice, once for a and once for b.) Compute the average of two float values. float average(float a, float b) { return (a + b) / 2; }

6 To activate (call) a function, write the function name, followed by a list of arguments: average (x, y) Arguments are used to supply information to a function; in this case, average needs to know which two numbers to average. The effect of the call average (x, y) is to copy the values of x and y into the parameters a and b, then execute the body of average. An argument doesn't have to be a variable, any expression of the proper type will do, average (5.1, 8.9) or average (x/2, y/3 ). Arrays can even be used as arguments. Every function has an executable part, called the body, which is enclosed in braces. The body of average consists of a single return statement. Executing this statement causes the function to "return" to the place from which it was called; the value of (a + b) / 2 will be returned by the function. float average(float a, float b) { return (a + b) / 2;/* BODY */ }

7 Note that the return value of average is not saved anywhere. The program prints it and then discards it. This below statement calls average, then saves its return value in the variable avg. avg = average (x,y); Put the call of average in the place where we need to use the return value. For example, we could write printf("Average: %g\n", average(x, y) ) ; to compute the average of x and y and then print it. This statement has the following effect: 1. It calls the average function, passing x and y as arguments. 2. average executes its return statement, returning the average of x and y. 3. printf prints the value that average returns. (The return value of average becomes one of printf's arguments.)

8 main() { float x, y, z; printf("Enter three numbers: "); scant("%f%f%f", &x, &y, &z) ; printf("Average of %g and %g: %g\n", x, y, average(x, y)); /* CALL */ printf("Average of %g and %g: %g\n", y, z, average(y, z) ) ; printf("Average of %g and %g: %g\n", x, z, average(x, z) ) ; return 0; } /* Computes pairwise averages of three numbers #include float average(float a, float b); /* DECLARATION */ float average(float a, float b)/* DEFINITION */ { return (a + b) / 2; } Enter three numbers: 3.5 9.6.10.2 Average of 3.5 and 9.6: 6.55 Average of 9,6 and 10.2: 9.9 Average of 3.5 and 10.2: 6.85

9 The difference between a parameter and an argument. Parameters appear in function definitions; they're dummy names that represent values to be supplied when the function is called. Arguments are expressions that appear in function calls. In C, arguments are passed by value: When a function is called, each argument is evaluated and its value assigned to the corresponding parameter.

10 Program Termination Since main is a function, it must have a return type. We've never specified main's return type, which means that it is int by default. We can make the return type explicit if we choose: int main() { return 0; } The value returned by main is a status code. It can be tested when the program terminates. Main should return 0 if the program terminates normally; To indicate abnormal termination, main should return a value other than 0.

11 The exit Function Executing a return statement in main is one way to terminate a program. Another is calling the exit function, which belongs to. The argument passed to exit has the same meaning as main's return value: Both indicate program status at termination. For normal termination pass 0: exit(O) ; /* normal termination */ C allows us to pass EXIT_SUCCESS instead of 0 (the effect is the same): exit(EXIT_SUCCESS) ; /* normal termination */ Passing EXIT_FAILURE indicates abnormal termination: exit(EXIT_FAILuRE); /* abnormal termination */ The difference between return and exit is that exit can be called from any function, not just from main. Some programmers use exit exclusively so that a pattern-matching program can easily locate all exit points in a program.

12 Recursive Functions A function is recursive if it calls itself For example, the following function computes n! recursively, using the formula n! = nx(n- 1)! int fact(int n) {. if (n <= 1) return 1; else return n * fact(n-l);/* RECURSIVE CALL TO FACT */ } An example to look at QUICKSORT Algorithm pp. 173-176.


Download ppt "Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions."

Similar presentations


Ads by Google