170 likes | 237 Views
Learn about defining, calling, and using functions in programming, including void and non-void functions, handling arguments, return statements, and program termination.
E N D
Example: Printing a Message • /* Illustrates functions with no arguments */ • #include <stdio.h> • void print_pun(void) • { • printf("To C, or not to C: "); • printf("that is the question.\n"); • } • int main(void) • { • print_pun(); • return 0; • }
Example: Counting Down • /* Illustrates functions with arguments */ • #include <stdio.h> • void print_count(int n) • { • printf("T minus %d and counting\n", n); • } • int main(void) • { • int i; • for (i = 10; i > 0; --i) • print_count(i); • return 0; • }
Example: Finding the Larger of Two Numbers • /* Illustrates functions that return a value */ • #include <stdio.h> • int max(int a, int b) • { • if (a > b) • return a; • else • return b; • } • int main(void) • { • int i, j; • printf("Enter two numbers: "); • scanf("%d%d", &i, &j); • printf("The larger number is %d\n", max(i, j)); • return 0; • }
Defining a Function • A function definition has the following appearance: result-type function-name ( parameters ) { declarations statements } • If a function does not return a value, its result type should be specified to be void. • If the result type is omitted, the function is assumed to return a value of type int. • If a function has no parameters, the word void should appear between the parentheses.
Calling a Function • To call a function, give the function name followed by a list of arguments (in parentheses). • A call of a void function must be a statement: print_pun(); print_count(i); • A call of a non-void function returns a value that can be stored in a variable, tested, printed, or used in some other way: k = max(i, j); if (max(i, j) > 10) ... printf("The larger number is %d\n", max(i, j));
Calling a Function • The value returned by a non-void function can always be discarded if desired: max(i, j); /* return value is discarded */ To make it clear that the return value is deliberately being discarded, it is possible to put (void) before the call: (void) max(i, j); /* return value is discarded */ • Warning: A call of a function with no arguments must include a pair of empty parentheses. Without the parentheses, the function is not called: f; /* wrong; f is not called */
Arguments • All arguments are passed by value. In other words, changing the value of a parameter affects only the function’s local copy. void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } ... swap(x, y); /* the values of x and y are not changed */
Arguments • If the type of an argument doesn’t match the type of the matching parameter, the argument will be converted to the proper type automatically: #include <stdio.h> float square(float x) { return x * x; } int main(void) { int i; i = 5; printf("The answer is %g\n", square(i)); return 0; }
Arguments • Warning: Automatic conversion of arguments takes place only if the compiler has previously seen a definition of the function.
Array Arguments • When a parameter is a one-dimensional array, the length of the array need not be specified: int find_largest(int a[], int n) { int i, max; max = a[0]; for (i = 1; i < n; i++) if (a[i] > max) max = a[i]; return max; }
Array Arguments • find_largest is called in the following way: #define N 100 int i, b[N]; i = find_largest(b, N);
return Statement • The return statement in a non-void function has the form return expression ; The expression is often just a constant or variable, but may be more complicated. • Executing a return statement has two effects: The function immediately returns to where it was called. The value of the specified expression is returned as the value of the function. • If the type of the expression doesn’t match the function’s return type, the expression will be implicitly converted to the return type.
return Statement • A function may contain more than one return statement: int max(int a, int b) { if (a > b) return a; else return b; }
Returning from main • main is a function like any other. Its return type, by default, is int. • The value returned by main is a status code that can be tested when the program terminates. This feature is useful in batch files and shell scripts. • By convention, main returns 0 if the program terminates normally. To indicate abnormal termination, main returns a value other than 0 (typically 1).
The exit Function • Another way to terminate a program is to call the exit function. The argument to exit should be either EXIT_SUCCESS or EXIT_FAILURE: exit(EXIT_SUCCESS); /* normal termination */ exit(EXIT_FAILURE); /* abnormal termination */ Calling exit with 0 as the argument has the same effect as calling exit with EXIT_SUCCESS as the argument.
The exit Function • Inside main, the call exit(expression); is equivalent to return expression; The difference between exit and return is that exit can be called from any function, not just from main. • Note: Programs that use exit should contain the following line: #include <stdlib.h>