Presentation is loading. Please wait.

Presentation is loading. Please wait.

Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).

Similar presentations


Presentation on theme: "Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters)."— Presentation transcript:

1 Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters). New data objects are created: Call-by-reference parameters can pass information both ways (input/output parameters). New labels are assigned to existing data objects.

2 Documentation Each function should be documented with a comment that states what the input or inputs to the function are, what the output or outputs are, and what the function does. For example: void findMinMax(int a, int b, int& min, int& max) { // Sets min to the min of a and b. // Sets max to the max of a and b. }

3 Local Variables Functions, including the main function, may have local variables. The corresponding data object is created at the point of the variable definition, and continues until the function finishes. Blocks of code may also have local variables. The corresponding data object exists until the block is completed. We've seen examples of blocks in if- statements, switch-statements, and loops.

4 Example { int i = 5; // declare/define i. int j = 10; // declare/define j.... } // The data objects corresponding to i and j // go away when the block is complete.

5 Scope Rules Since different data objects can have the same label (identifier, variable) and yet still be different objects, the computer must be able to associate a name with the right data object.

6 Example int main(void) { int a = 5; if(a > 0) { int a = 10; cout << "a = " << a << endl; } cout << "a = " << a << endl; } Output: a = 10 a = 5

7 The Rules A variable that is declared within the body of a function definition (or within a block) is said to be local to that function (or block) or to have that function (or block) as it scope. The declaration must occur before the use of the variable. The same name can be used in different scopes. The closest containing scope is the only that applies.

8 Pitfalls It is an error to declare the same variable twice in the same scope. It is bad practice to declare a local variable with the same name as the parameter of the function.

9 Loops In a ANSI standard C++ compiler, the scope of a variable declared in a for loop is local to that loop. for (int n = 1; n <= 10; n++) sum += n; // n is no longer defined here. If you want n to be defined after the loop, do this: int n; for(n = 1; n <=10; n++) sum += n; // n is defined here. Warning: Not all compilers are ANSI standard.

10 Global Variables You can also defined a variable globally but putting the declaration/definition outside of any function (including main). A global variable must be declared before it is used. A global variable may be used anywhere in the program, including inside of functions (once declared). You can have a local variable with the same name. They should be used sparingly, if at all.

11 Global Constants A constant can be declared globally, but putting the declaration outside of any function. A global constant can be used anywhere in the program once it is declared. Global constants are fine to use. Example: const double PI = 3.1415926; The convention is to use all capital letters for global constants.

12 Constant Reference Parameters You can declare a call-by-reference parameter to be constant. The identifier is still attached to the data object of the corresponding argument, and that value is used in the function, but the function is not allowed to change the value of the data object! Why would we ever want to do this, instead of just using a call-by-value parameter?

13 Example of Call-by-Reference Often we want to swap two values: int a = 5; b = 10; int t; t = a; a = b; b = t; This is commonly done as a function, called swap: void swap(int& x, int& y) { int t = x; x = y; y = t; }

14 How this works Suppose we have the following call: int a = 5; b = 10; swap(a,b); Two data objects are created: When the function, swap, is called, the identifiers x and y become additional names for these data objects:

15 How this works (cont'd) In the body of swap, an additional data object is created for the local variable, t, and initialized to 5 The code of swap is done, and the values are exchanged: Finally, the function swap finishes, and the additional labels and data object disappear:

16 Pitfalls A common mistake is to use a call-by-value parameter (forgetting the &) instead of a call-by- reference parameter for an output variable. Changing the value of the parameter in the function does not change the value of the corresponding argument in the calling function.

17 Exercise Write a function, roots, that returns the two roots of a quadratic equation. Recall that a quadratic equation is one of the form a* x 2 + b * x + c = 0, and the two roots are of the form: -b ± sqrt(b 2 – 4ac) 2a The function should have five parameters: three input parameters ( a,b,c ) and two output parameters ( r1,r2 ), and return type void.


Download ppt "Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters)."

Similar presentations


Ads by Google