Presentation is loading. Please wait.

Presentation is loading. Please wait.

User-Defined Functions (cont’d) - Reference Parameters.

Similar presentations


Presentation on theme: "User-Defined Functions (cont’d) - Reference Parameters."— Presentation transcript:

1 User-Defined Functions (cont’d) - Reference Parameters

2 CSCE 1062 Outline  Exercise  Passing by value  Scope of variables  Reference parameters (section 6.1)

3 CSCE 1063 Tracing Exercise #include using namespace std; int Blend( int red, int green ); // prototype void main() { int red = 5, blue = 3; blue = Blend(blue, red); cout << red << ' ' << blue << '\n'; blue = Blend(red, blue); cout << red << ' ' << blue << '\n'; } int Blend( int red, int green ) // parameters passed by value { int yellow;// local variable in Blend function cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’; yellow = red + green; cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’; return (yellow + 1); } Execution

4 CSCE 1064 Passing by Value main function data area actual arguments: red 5 blue 3 Blend function data area (1 st call) formal arguments: red 3 green 5 Local arguments: yellow 8

5 CSCE 1065 Tracing Exercise (cont’d) main Blend (1 st call) Blend (2 nd call) redblue 53 9 15 Output: enter Blend 3 5 leave Blend 3 5 5 9 enter Blend 5 9 leave Blend 5 9 5 15 redgreenyellow 358 redgreenyellow 5914

6 CSCE 1066 Scope of Variables  Scope - where a particular meaning of a variable identifier is visible or can be referenced  Local - can be referred to only within a program segment or function  In a program segment (localized declarations of variables) for (int i = 1; i <= 10; ++i) cout << “*”; Commonly used for loop control variables Declared at point of first reference Value has meaning (i.e. can be referenced) only inside loop segment.

7 CSCE 1067 Scope of Variables (cont’d)  In a function this applies to formal argument names constants and variables declared within the function  Global - can be referred to within all functions  useful especially for constants  must be used with care

8 CSCE 1068 Listing 3.15 Outline of program for studying scope of variables

9 CSCE 1069 Scope of Variables (cont’d)  Global variables MAX & LIMIT are visible to main, one, funTwo functions  Local variable localVar in main function visible only to main function  Local variables anArg, second & oneLocal in one function are visible only to one function  Local variables one, anArg & localVar in funTwo function are visible only to funTwo function

10 CSCE 10610 Passing by Reference Example #include using namespace std; int Blend( int& red, int green ); // prototype void main() { int red = 5, blue = 3; Blend(blue, red); cout << red << ' ' << blue << '\n'; Blend(red, blue); cout << red << ' ' << blue << '\n'; } void Blend(int& red, int green) // green parameter passed by value, // while red parameter passed by reference { int yellow;// local variable in Blend function cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’; yellow = red + green; cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’; red = yellow + 1; }

11 CSCE 10611 Passing by Reference main function data area actual arguments: red 5 blue 3 Blend function data area (1 st call) formal arguments: red address of blue green 5 Local arguments: yellow 8

12 CSCE 10612 Passing by Reference Example (cont’d) main Blend (1 st call) Blend (2 nd call) redblue 53 9 15 Output: enter Blend 3 5 leave Blend 3 5 5 9 enter Blend 5 9 leave Blend 5 9 15 9 redgreenyellow 58 redgreenyellow 914

13 CSCE 10613 User Defined Functions Can:  return no value  type void  return exactly one value  function type  return statement  return more than one value  type void or function type  reference parameters

14 CSCE 10614 Reference Parameters  Formal parameter data type directly followed by & indicate a parameter passed by reference

15 CSCE 10615 Listing 6.1 Function to compute sum and average

16 CSCE 10616 Listing 6.1 Function to compute sum and average (continued)

17 CSCE 10617 User-Defined Function computeSumAve  Two function input parameters  num1, num2  Two function output parameters  sum, average  & indicates function output parameters  Function call computeSumAve(x, y, sum, mean);

18 CSCE 10618 Argument Correspondence Actual Argument x y sum mean Corresponds to Formal Argument num1 (fn. input) num2 (fn. input) sum (fn. output) average (fn. output)

19 CSCE 10619 Call-by-Value and Call-by- Reference Parameters  & between type and identifier defines a parameter as function output mode (pass by reference)  no & in a parameter’s declaration identifies parameter as fuction input mode (pass by value)  Compiler uses information in parameter declaration list to set up correct argument-passing mechanism

20 CSCE 10620 Figure 6.1 Data areas after call to computeSumAve (before execution)

21 CSCE 10621 Figure 6.2 Data areas after execution of computeSumAve

22 CSCE 10622 Notes on Call-by-Reference  Place the & only in the formal parameter list - not in the actual parameter list  Place the & also in the prototype: void computeSumAve(float, float, float&, float&);  Note that this is a void function

23 CSCE 10623 When to Use a Reference or a Value Parameter  If information is to be passed into a function and doesn’t have to be returned or passed out of the function, then the formal parameter representing that information should be a value parameter (function input parameter).  If information is to be returned to the calling function through a parameter, then the formal parameter representing that information must be a reference parameter (function output parameter).

24 CSCE 10624 When to Use a Reference or a Value Parameter (cont’d)  If information is to be passed into a function, perhaps modified, and a new value returned, then the formal parameter representing that information must be a reference parameter (input/output parameter)

25 CSCE 10625 Next lecture we will talk more about Value and Reference Parameters


Download ppt "User-Defined Functions (cont’d) - Reference Parameters."

Similar presentations


Ads by Google