DOC PREVIEW
UW CSE 142 - Function Parameters

This preview shows page 1-2-3-4-24-25-26-50-51-52-53 out of 53 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

University of Washington Computer Programming IOverviewPowerPoint PresentationThe Client Wants a ChangeCan we Generalize?Slide 6Slide 7Code for the Modified FunctionSlide 9Returned ValuesSlide 11Slide 12More on returnSlide 14Discussion QuestionsSlide 17Slide 18Review: Function Control FlowSlide 20Slide 21Slide 22Slide 23Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Full Program, Page I of 2Full Program, Page 2 of 2Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Order in the ProgramSlide 50Function PrototypesSlide 52#include <stdio.h>Compilers, Linkers, etc.Slide 55Slide 56G2-101/14/19University of WashingtonComputer Programming ILecture 8:Function Parameters© 2000 UW CSEG2-2OverviewMany concepts in this lecture! The most memorable:Function parameters and argumentsReturn values, return types, and the return statementLocal variablesFunction prototypes and header filesLots of new terminolgy, tooNear the end of the lecture:An extended program, tracedG2-3Refresher: Printing a Banner/* write separator line on output */void PrintBannerLines (void){printf(“***************\n”);printf(“***************\n”);}Our original banner program called this function to print a very simple bannerG2-4The Client Wants a ChangeSuppose 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 elseWe could write an additional function that prints 5 rows of asterisks, or...G2-5Can 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 elseWe 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-6Can we Generalize?Can we modify the function so that instead ofprint two rows of asterisksit will:print N rows of asterisksWhere N is the number of rows that we want “this time” when we call itN is information that the function needs to know#include <stdio.h>int main(void){PrintBannerLines(5);/* produce some output */PrintBannerLines(2);/* produce final output */ PrintBannerLines(5);return 0;}The value in the parentheses is called the “parameter” of this call.5Code for PrintBannerLinesG2-8Code for the Modified FunctionThe 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 variableThe full solution won’t be shown now. It requires a feature called “iteration” that we will cover later. We’ll see parameters in other examples.G2-9A New Example ProblemSpecification: Write a function which, given the radius, computes and returns the area of a circle with that radiusThe new wrinkle here is that the function must “return” a valueG2-10Returned ValuesParameters are a way for the calling routine to “send data” to the functionThe new concept, return values, are the opposite, a way for the function to send data back to the calling routineG2-11area Function, SolvedSpecification: Write a function which, given the radius, returns the area of a circle with that radiusNew 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-12Void Parameters, Non-void ReturnsThis function gives back a number that it generates internally, without the need for a parameter from the caller./* return a “random” number. */double GenRandom (void){ double result; result = ... return result;}function type (type of returned value). We say “GenRandom( ) is a function of type double” or “GenRandom( ) returns a double.”return statementreturned valuelocal variable – exists only while function is executingG2-13More on returnFor 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 expressionreturn is a C statement. It is not a function!G2-14Calling a Non-Void FunctionA value-returning function can be used anywhere an expression of the same type can be usedint 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-16Discussion Questions1. 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 asdouble calculation (void) {…could it contain this statement?return;4. If a function starts off asvoid printfBankBalance (void) {…could it contain this statement?return currentBalance;G2-17Matching up the ArgumentsRule: 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.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;}parameter passingG2-18More Terminology ConfusionMany 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-19Review: Function Control FlowSome time ago we described the basic flow.We can now give a much more detailed account of how this flow worksG2-20Control and Data FlowWhen 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-21Control and Data Flowint main (void){ double x, y, z; y = 6.0; x = area(y/3.0) ; .... .... z =


View Full Document

UW CSE 142 - Function Parameters

Download Function Parameters
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 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 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?