Unformatted text preview:

University of Washington Computer Programming I Lecture 8 Function Parameters 2000 UW CSE 01 14 19 G2 1 Overview Many concepts in this lecture The most memorable Function parameters and arguments Return values return types and the return statement Local variables Function prototypes and header files Lots of new terminolgy too Near the end of the lecture An extended program traced G2 2 Refresher Printing a Banner Our original banner program called this function to print a very simple banner write separator line on output void PrintBannerLines void printf n printf n G2 3 The Client Wants a Change Suppose we now want to change the program it should now print 5 rows of asterisks when it starts and when it finishes but print the original 2 line banner everywhere else We could write an additional function that prints 5 rows of asterisks or G2 4 Can we Generalize Suppose we now want to change the program it should now print 5 rows of asterisks when it starts and when it finishes but print the original 2 line banner everywhere else We could write an additional function that prints 5 rows of asterisks or Could we somehow generalize PrintBannerLines Could we make the same function do double duty G2 5 Can we Generalize Can we modify the function so that instead of print two rows of asterisks it will print N rows of asterisks Where N is the number of rows that we want this time when we call it G2 6 N is information that the function needs to know include stdio h int main void PrintBannerLines 5 5 produce some output PrintBannerLines 2 Code for PrintBannerLines produce final output PrintBannerLines 5 return 0 The value in the parentheses is called the parameter of this call Code for the Modified Function The function will start off this way void PrintBannerLines int n n is the parameter of the function n can be used inside the function just like a variable The full solution won t be shown now It requires a feature called iteration that we will cover G2 8 later We ll see parameters in other examples A New Example Problem Specification Write a function which given the radius computes and returns the area of a circle with that radius The new wrinkle here is that the function must return a value G2 9 Returned Values Parameters are a way for the calling routine to send data to the function The new concept return values are the opposite a way for the function to send data back to the calling routine G2 10 area Function Solved Specification Write a function which given the radius returns the area of a circle with that radius New features 1 The return statement sends the value back 2 The type of the returned value is stated before the function name Find area of circle with radius r double area double r return 3 14 r r G2 11 Void Parameters Non void Returns This function gives back a number that it generates internally without the need for a parameter from the caller function type type of returned value We say GenRandom is a function of type double or GenRandom returns a double return a random number double GenRandom void double result result return result local variable exists only while function is executing G2 12 returned value return statement More on return For void functions return causes control flow to return to the statement following the call in the caller For functions that return a value return expression causes control flow to return to the caller The function call is replaced with the returned value Note no parentheses are needed on the expression G2 13 return is a C statement It is not a function Calling a Non Void Function A value returning function can be used anywhere an expression of the same type can be used int main void double firstRandom secondRandom double result firstRandom GenRandom secondRandom GenRandom result firstRandom secondRandom printf the value of f f is f firstRandom secondRandom result return 0 G2 14 Discussion Questions 1 Can you have more than one return inside a function 2 Does a return statement have to be the last statement of a function 3 If a function starts off as double calculation void could it contain this statement return 4 If a function starts off as void printfBankBalance void could it contain this statement return currentBalance G2 16 Matching up the Arguments Rule The function call must include a matching argument for each parameter When the function is executed the value of the argument becomes the initial value of the parameter parameter passing int main void z 98 76 x 34 575 area z 2 0 return 0 Find area of circle with radius r double area double r return 3 14 r r G2 17 More Terminology Confusion Many people use the term formal parameter instead of parameter and actual parameter instead of argument We will try to stick to parameter and argument for simplicity but the other terminology will probably slip in from time to time People often refer to replacing a parameter with the argument in a function call as passing the argument to the function G2 18 Review Function Control Flow Some time ago we described the basic flow We can now give a much more detailed account of how this flow works G2 19 Control and Data Flow When a function is called 1 Memory space is allocated for the function s parameters and local variables 2 Argument values are copied 3 Control transfers to the function body 4 The function executes 5 Control and return value return to the point of call G2 20 Control and Data Flow int main void double x y z y 6 0 x area y 3 0 z 3 4 area 7 88 Find area of circle with radius r double area double n 2 0 12 56 7 88 return 3 14 r r 194 976 return 0 G2 21 Style Points The comment above a function must give a complete specification of what the function does including the significance of all parameters and any returned value Someone wishing to use the function should be able to cover the function body and find everything they need to know in the function heading and comment Compute area of circle with radius r double area double r return 3 14 r r G2 22 Multiple Parameters A function may have more than one parameter Arguments must match parameters in number order and type double gpt gpa gpt 3 0 3 3 3 9 gpa avg gpt 3 double avg double total int count return total double count arguments parameters G2 23 Where Are We We have seen all of the basic concepts for how a function communicates with the outside world through parameters and return values We know the syntax involved as well as the logical concepts There is still a topic centered with the


View Full Document

UW CSE 142 - Function Parameters

Loading Unlocking...
Login

Join to view Function Parameters 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 Function Parameters 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?