170 likes | 256 Views
User-Written Functions. Lecture 12. Things to Look Out For. Vocabulary Prototypes Definition Call Return type Arguments or parameters Local variables Pass by value Pass by reference. User-Written Functions.
E N D
User-Written Functions Lecture 12 Winter Quarter
Things to Look Out For • Vocabulary • Prototypes • Definition • Call • Return type • Arguments or parameters • Local variables • Pass by value • Pass by reference Winter Quarter
User-Written Functions • So far, we have written only one function ourselves. That function is called "main ( )". • The syntax used has been as follows: int main ( ) { /* Declarations */ /* Statements */ return 0; } • The word int means that the function is expected to return an integer value to another function. Winter Quarter
User-Written Functions • The parentheses, ( ), both indicate that main is a function, AND provide a place for any variable names (arguments) that could be sent as values from another function. Since main is the main program function, it will normally not have a parameter list. (Who calls the main function anyway?) • Most, but not all, other functions have parameter lists. Some such, as rand ( ), do not since they need no data or values from the calling function. Winter Quarter
Writing User-Written Functions • There are typically three different types of statements needed to properly set-up and make use of a user-written function. They are: 1. The function prototype statement. 2. The first line of the function definition. 3. The calling statement. Winter Quarter
1. The Function Prototype Statement. • The function prototype's format is (note semicolon): return-value-type function-name (parameter-list) ; • The return-value-type may be any legal data type such as int, long, float, double, char, or it may be void. • The parameter list is a list of data-types for the arguments and, optionally, the names of arguments. • Example: float sum ( int , int , int ) ; /* OR */ float sum ( int a, int b, int c ) ; Winter Quarter
2. The First Line of the Function Definition. • The first line of the function definition is the same as the examples of the function prototype except that the SEMICOLON IS ELIMINATED and variable or argument NAMES ARE REQUIRED. • Example: float sum ( int a, int b, int c ) • Here, the function sum is of type float and has three calling parameters, all of which are of type int. • The variables a, b, and c take on the values of the variables or constants used in the calling statement. Winter Quarter
The Body of the Function • A complete function consists of the the function definition and the body of the function in the { }. • Example: float sum (int a, int b, int c) { float total; total = a + b + c ; return total; } • Note: The function has returnstatement because it has a return type. Winter Quarter
The Body of the Function float sum (int a, int b, int c) { float total; total = a + b + c ; return total; } • The names a, b, and c are known only inside sum. • Likewise, any variables, like total, declared within the function are local variables, and they are known only inside the function in which they are defined. Winter Quarter
3. The Calling Statement. • The function call (or invocation) is like the first line of the function definition except NO DATA TYPES are shown and constants may be used instead of variable names. The valued returned by a function is normally assigned to a variable in the calling function. • Example: value = sum ( i, j, k ) ; or value = sum ( 5, 6.73, 2 ) ; Winter Quarter
Factorial Function Main Program #include <stdio.h> long factorial (int x ) ; int main ( ) { int k; for( k=0 ; k<=10 ; k++ ) { printf("%2d factorial is %ld\n", k , factorial (k)) ; } return 0 ; } ? ? Winter Quarter
Factorial Function long factorial (int x ) { long fact = 1;int k; if( x < 0 )return 0; else if( x==0 || x==1 )return 1; else { for( k = 2 ; k <= x ; k++ ) fact = fact * k ; return fact ; } } Winter Quarter
Factorial Program Output 0 factorial is 1 1 factorial is 1 2 factorial is 2 3 factorial is 6 4 factorial is 24 5 factorial is 120 6 factorial is 720 7 factorial is 5040 8 factorial is 40320 9 factorial is 362880 10 factorial is 3628800 Winter Quarter
The Next Slide is VERY VERY Important!! Winter Quarter
#include <stdio.h> void swap (int a, int b ) ; int main ( ) { int a = 5, b = 6; printf("a=%d b=%d\n",a,b); swap (a, b); printf("a=%d b=%d\n",a,b); return 0; } void swap (int a, int b ) { int temp; temp = a; a = b; b = temp; printf("a=%d b=%d\n", a, b ); } a=5 b=6 a=6 b=5 a=5 b=6 User-Written Functions (example): Winter Quarter
Call By Value vs. Call By Reference • Two ways in which variables are passed: • Call by value • Call by reference • "Call by value" means function only gets a copy of the value of the calling argument. The called function can not change the value back in the calling routine. This is normally how it is in C. • "Call by reference" means the function gets the original argument, and thus the called function can change the value of the variable back in the calling routine. Must use pointers in C to do this. Winter Quarter
Problem G11 • Problem G11 calculates the values of sine for angles between 0 and 90 degrees. • You are provided with a partial program that has a main function and a user written sine function. • You are to complete the user written sine function (mysine(x) ). • The main program will compare the calculations or your mysine function with the sin function from the math.h library. Winter Quarter