g2 1
play

G2-1 Two Key Features Further details: void 1. The name of the - PDF document

Big Idea for Code: Functions Deceptively Simple Big Idea One idea One definition, many uses One idea One definition, many uses One idea Identify a sub-problem that has to be solved in your One idea One definition, many uses


  1. Big Idea for Code: Functions Deceptively Simple Big Idea One idea ⇒ One definition, many uses One idea ⇒ One definition, many uses One idea • Identify a “sub-problem” that has to be solved in your One idea ⇒ One definition, many uses program One idea ⇒ One definition, many uses Many • Choose a name to represent “ the solution of that Name One idea ⇒ One definition, many uses problem by code” One One idea ⇒ One definition, many uses • Write that solution code (only once) definition One idea ⇒ One definition, many uses • Whenever you see that same sub-problem again, Many use the function name to say “ go to that code now to Uses One idea ⇒ One definition, many uses take care of this problem, and don’t come back until One idea ⇒ One definition, many uses you’re done ” G2-1 G2-2 Some C Functions Library functions We have already seen and used several functions: • Pre-written functions are commonly packaged in "libraries” Function int main (void) • Every standard C compiler comes with a set of standard definition libraries { for main( ) • Remember #include <stdio.h> ? return 0; – Tells the compiler you intend to use the “standard } I/O library” functions – printf and scanf are in the standard I/O library printf ("control", list); – So are lots of other I/O related functions Calls to the functions • There are (many) other useful functions in other libraries printf( ) and scanf( ) scanf ("control", &list); G2-3 G2-4 Syntax for PrintBannerLines #include <stdio.h> int main(void) The code named PrintBannerLines { /* produce some output */ PrintBannerLines(); /* write separator line on output */ printf("********************\n"); /* produce more output */ printf("********************\n"); void PrintBannerLines (void) { PrintBannerLines(); printf("***************\n"); /* produce more output */ printf("***************\n"); PrintBannerLines(); } /* produce final output */ G2-5 G2-6 return 0 ; } G2-1

  2. Two Key Features Further details: void 1. The name of the function and The keyword void has two different 2. the function body: code that is to be executed roles in this function definition. when the function is called. indicates that the function does not return a value. function name /* write separator line on output*/ /* write separator line on output*/ heading comment void PrintBannerLines (void) void PrintBannerLines (void) function body { { (statements to be indicates that the printf("***************\n"); executed). printf("***************\n"); function has no parameters. printf("***************\n"); A function can have printf("***************\n"); ANY number of ANY } } G2-7 G2-8 kind of statements. Providing an Input to the Function: Using PrintBannerLines Parameters #include <stdio.h> Can we modify the function so that instead of void PrintBannerLines (void) print two rows of asterisks The definition of { the function must (callee decides) printf("***************\n"); precede all calls it will: printf("***************\n"); to it in the file. print N rows of asterisks } (caller decides) int main (void) { where N is the number of rows that we want “this Empty ( ) is /* produce some output */ … time” when we call it required when a PrintBannerLines( ); parameter-less ... return 0; (void) function is } G2-9 G2-10 N is information that the function needs to know called. Code for the Modified Function #include <stdio.h> int main(void) The function will start off this way: { PrintBannerLines(5); 5 void PrintBannerLines (int n) /* produce some output */ { Code for ... PrintBannerLines(2); PrintBannerLines /* produce final output */ n is the “parameter” of the function. n can be The value in the parentheses is PrintBannerLines(5); used inside the function just like a variable called the “parameter” of this call. (The full solution won’t be shown now. It requires a feature return 0; called “iteration” that we will cover later. We’ll see parameters } G2-12 in other examples.) G2-2

  3. Some Terminology Confusion Parameters are New Variables!!! Many people use the term formal parameter instead of parameter and actual parameter instead of argument . We will try to stick to void PrintBannerLines (int n) parameter and argument for simplicity, but the PrintBannerLines(num); { other terminology will probably slip in from time to … … time. n = n – 1; 5 = ….? PrintBannerLines(5); … People often refer to replacing a parameter with the return; argument in a function call as “passing the } argument to the function”. G2-13 G2-14 Passing Results Back to the Caller Returned Values Specification: Write a function that, given the Parameters are a way for the calling radius, computes the area of a circle with that routine to “send data” to the function radius. The new concept, return values, are the What should it do with the result? opposite, a way for the function to send data back to the calling routine Caller Area function Please compute the area of a circle of radius 1.7. Please compute the area of a circle of radius 1.7. It’s 9.08 newArea = Area(1.7); G2-15 newArea = Area(1.7); return 3.14 * r * r; G2-16 Returned values Control and Data Flow int main (void) Type of value passed in { /* Find area of circle double x, y, z; Type of value passed back with radius r */ y = 6.0; double Area (double r) 2.0 /* return area of circle with radius r */ x = Area(y/3.0) ; { double Area (double r) .... 12.56 return 3.14 * r * r; { 7.88 .... } return 3.14 * r * r; z = 3.4 * Area(7.88) ; 194.976... } .... “Local variable” initialized return 0; In the calling statement, } to caller’s argument G2-17 G2-18 the call is evaluated as this value G2-3

  4. More on return Discussion Questions For void functions: 1. Can you have more than one return inside a function? return; causes control flow to return to the statement following the call in the caller 2. Does a return statement have to be the last statement of a function? 3. If a function starts off as For functions that return a value: double calculation (void) {… return expression; causes control flow to return to the could it contain this statement? caller. The function call is “replaced” with the returned return; value. 4. If a function starts off as totalArea = Area(1.7) + Area(3.4); void printfBankBalance (void) {… could it contain this statement? Note: no parentheses are needed around the expression G2-19 G2-20 return currentBalance; Return is a C statement. It is not a function call Matching Up Types Multiple Parameters The actual arguments must be of the type of the parameter. A function may have more than one parameter The returned value will be of the type given before the Arguments must match parameters in number, function’s name. order, and type The “usual” conversion rules apply (as in expressions). double gpt, gpa; double Avg (double total, int count) gpt = 3.0 + 3.3 + 3.9; int main (void) { gpa = Avg ( gpt, 3 ); { ... return total / (double) count ; ... z = 98.76; } /* Find area of circle with radius r */ x = 34.575 * area ( z/2.0 ); double area (double r) { … arguments parameters G2-21 G2-22 return 3.14 * r * r; return 0; } } Local Variables A Function with Local Variables A function can define its own local variables. The locals have meaning only within /* return area of circle with the function. radius r */ parameter Local variables are created when double CircleArea (double r) the function is called. { local variables double rsquared, area; Local variables cease to exist when rsquared = r * r ; the function returns. area = 3.14 * rsquared ; return area; } Parameters are also local. G2-23 G2-24 G2-4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend