1 / 17

User-Written Functions

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.

marilyn
Download Presentation

User-Written Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. User-Written Functions Lecture 12 Winter Quarter

  2. Things to Look Out For • Vocabulary • Prototypes • Definition • Call • Return type • Arguments or parameters • Local variables • Pass by value • Pass by reference Winter Quarter

  3. 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

  4. 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

  5. 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

  6. 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

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

  8. 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

  9. 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

  10. 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

  11. 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

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

  13. 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

  14. The Next Slide is VERY VERY Important!! Winter Quarter

  15. #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

  16. 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

  17. 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

More Related