OSU ENGR H192 - Lecture 12 - User Written Functions

Unformatted text preview:

User-Written FunctionsThings to Look Out ForSlide 3Slide 4Writing User-Written Functions1. The Function Prototype Statement.2. The First Line of the Function Definition.The Body of the FunctionSlide 93. The Calling Statement.Factorial Function Main ProgramFactorial FunctionFactorial Program OutputThe Next Slide is VERY VERY Important!!User-Written Functions (example):Call By Value vs. Call By ReferenceLect 12 P. 1 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionUser-Written FunctionsLecture 12Lect 12 P. 2 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionThings to Look Out For•Vocabulary–Prototypes–Definition–Call–Return type–Arguments or parameters•Local variables•Pass by value•Pass by referenceLect 12 P. 3 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionUser-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.Lect 12 P. 4 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionUser-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.Lect 12 P. 5 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionWriting 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.Lect 12 P. 6 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalition1. 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 ) ;Lect 12 P. 7 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalition2. 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.Lect 12 P. 8 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionThe 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 return statement because it has a return type.Lect 12 P. 9 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionThe Body of the Functionfloat 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.Lect 12 P. 10 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalition3. 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 ) ;orvalue = sum ( 5, 6.73, 2 ) ;Lect 12 P. 11 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFactorial 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 ;} ??Lect 12 P. 12 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFactorial Functionlong 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 ; }}Lect 12 P. 13 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFactorial 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 36288010 factorial is 3628800Lect 12 P. 14 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionThe Next Slide is VERY VERY Important!!Lect 12 P. 15 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionUser-Written Functions (example):#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


View Full Document

OSU ENGR H192 - Lecture 12 - User Written Functions

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 12 - User Written Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 12 - User Written Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 12 - User Written Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?