functions
play

Functions Function: Unit of operation Functions o A series of - PDF document

2/25/14 Functions Function: Unit of operation Functions o A series of statements grouped together with a given name Must have the main function C functions are stand-alone Based on slides from K. N. King and Dianna Xu Most


  1. 2/25/14 ¡ Functions • Function: Unit of operation Functions o A series of statements grouped together with a given name • Must have the main function • C functions are stand-alone Based on slides from K. N. King and Dianna Xu • Most programs contain multiple function definitions Bryn Mawr College CS246 Programming Paradigm o Must be declared/defined before being used Identify Repeated Code Identify Repeated Code int menuChoice() { int main() { int choice; int choice; printf( "1. Yes\n" printf("=== Expert System ===\n"); "0. No\n" printf("Question1: ...\n"); "Enter the number corresponding to your choice: "); printf( scanf("%d", &choice); "1. Yes\n" return choice; "0. No\n" } "Enter the number corresponding to your choice: "); scanf("%d", &choice); int main() { int choice; if (choice == 1) { /* yes */ printf("Question 2: ...\n"); printf("=== Expert System ===\n"); printf( printf("Question1: ...\n"); "1. Yes\n" choice = menuChoice(); "0. No\n" "Enter the number corresponding to your choice: "); if (choice == 1) { /* yes */ scanf("%d", &choice); printf("Question 2: ...\n"); /* skipped */ choice = menuChoice(); /* skipped */ Identify Similar Code Use Parameters to Customize void km_mile_conv(int choice) { int main() { int input; int choice; double km, mile; printf("Enter a %s value: ", choice==1?"mile":"km"); scanf("%d", &choice); scanf("%lf", &input); if (choice == 1) switch (choice) { printf("%f mile(s) = %f km(s)\n", input, input*1.6); case 1: else printf("Enter a mile value: "); Similar printf("%f km(s) = %f mile(s)\n", input, input/1.6); scanf("%lf", &mile); unit } km = mile * 1.6; int main() { printf("%f mile(s) = %f km\n", mile, km); break; int choice; scanf("%d", &choice); caes 2: switch (choice) { printf("Enter a km value: "); case 1: Similar scanf("%lf", &km); km_mile_conv(choice); mile = km / 1.6; unit break; printf("%f km = %f mile(s)\n", km, mile); caea 2: break; km_mile_conv(choice); default: break; More readable main printf("\n*** error: invalid choice ***\n"); /* more cases */ } } } } 1 ¡

  2. 2/25/14 ¡ Function-oriented Function Call • C came before OO concept void km_to_mile() { • C program resemble java programs with a single printf("Enter a mile value: "); scanf("%lf", &mile); giant class km = mile * 1.6; printf("%f mile(s) = %f km\n", mile, km); • C is procedural } o Program organization and modularization is int main() { achieved through function design o Carefully plan your function return type and km_to_mile(); parameter list km_to_mile(); o Write small functions! return 0; } Function Return and Parameters Use of return in void Functions • Exit from the function • The syntax for C functions is the same as Java methods void getinput() { int choice; • void keyword can be omitted while (1) { scanf("%d", &choice); void km_to_mile(void) { switch (choice) { } case 1: /* some action */ mile_to_km() { break; case 0: } return; /* exit from getinput */ } int main() { } int choice; } } The exit Function Function Prototype • Executing a return statement in main is one way to • A prototype is a /* function prototypes */ terminate a program. function declaration double km2mile(double); • Another is calling the exit function, which belongs to which includes the double mile2km(double); <stdlib.h> . return type and a list of int main() { } • The statement parameters /* actual function definitions */ return expression ; double km2mile(double k) { • A way to move function in main is equivalent to definitions after main } exit( expression ); double mile2km(double m) { • Need not name formal • To indicate normal termination, we ’ d pass 0: parameters } • exit(0); /* normal termination */ The difference between return and exit is that exit causes program termination regardless of which function calls it. • The return statement causes program termination only when it appears in the main function. 2 ¡

  3. 2/25/14 ¡ Array Arguments Array Arguments • When a function parameter is a one-dimensional • Example: array, the length of the array can be left unspecified: int sum_array(int a[], int n) { int f(int a[]){ /* no length specified */ int i, sum = 0; … } for (i = 0; i < n; i++) • We can supply the length—if the function needs it— sum += a[i]; as an additional argument. return sum; } • Since sum_array needs to know the length of a , we must supply it as a second argument. Array Arguments Array Arguments • The prototype for sum_array has the following • When sum_array is called, the first argument will be the name of an array, and the second will be its length: appearance: #define LEN 100 int sum_array(int a[], int n); • We can omit the parameter names if we wish: int main(void) { int sum_array(int [], int); int b[LEN], total; … total = sum_array( b , LEN); … } • Notice that we don ’ t put brackets after an array name when passing it to a function: total = sum_array(b[], LEN); /*** WRONG ***/ Array Arguments Array Arguments • Suppose that we ’ ve only stored 50 numbers in the • A function is allowed to change the elements of an b array, even though it can hold 100. array parameter, and the change is reflected in the corresponding argument. • We can sum just the first 50 elements by writing • A function that modifies an array by storing zero total = sum_array(b, 50); into each of its elements: • Be careful not to tell a function that an array argument is larger than it really is: void store_zeros(int a[], int n) { total = sum_array(b, 150); /*** WRONG ***/ int i; sum_array will go past the end of the array, for (i = 0; i < n; i++) causing undefined behavior. a[i] = 0; } 3 ¡

  4. 2/25/14 ¡ Array Arguments The return Statement • A non- void function must use the return • If a parameter is a multidimensional array, only the length statement to specify what value it will return. of the first dimension may be omitted. • If we revise sum_array so that a is a two-dimensional • The return statement has the form array, we must specify the number of columns in a : return expression ; #define LEN 10 • The expression is often just a constant or variable: int sum_two_dimensional_array(int a[][LEN], int n) return 0; { int i, j, sum = 0; return status; • More complex expressions are possible: for (i = 0; i < n; i++) for (j = 0; j < LEN; j++) return n >= 0 ? n : 0; sum += a[i][j]; return sum; } The exit Function The exit Function • Executing a return statement in main is one • The statement way to terminate a program. return expression ; • Another is calling the exit function, which in main is equivalent to belongs to <stdlib.h> . exit( expression ); • The argument passed to exit has the same • The difference between return and exit is that meaning as main ’ s return value: both indicate the exit causes program termination regardless of program ’ s status at termination. which function calls it. • To indicate normal termination, we ’ d pass 0: • The return statement causes program termination only when it appears in the main exit(0); /* normal termination */ function. Local/Global Variables Local Variables • Variables declared inside a function are local • Since C99 doesn ’ t require variable declarations to • Function arguments are local to the function passed come at the beginning of a function, it ’ s possible to for a local variable to have a very small scope: • A global variable is a variable declared outside of any function. • In a name conflict, the local int x = 0; int f(int x) { variable takes precedence int x = 1; • When local variable shadows return x; } function parameter? int main() { int x; x = f(2); } 4 ¡

Recommend


More recommend