Download presentation
Presentation is loading. Please wait.
Published byLeslie Dalton Modified over 9 years ago
1
Parameter passing mechanism: pass-by-value
2
Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately) simple by using constant values as parameters: In this webpage, we will discuss how to pass information stored in variables to a method. Specifically, we will study the pass-by-value mechanism r = ToolBox.min( 1.0, 4.0 );
3
Example: passing information stored in variables Consider the following program:
4
Example: passing information stored in variables (cont.) Question to ponder: There are quite a few ways to allow (enable) you to accomplish this "passing" The possible answers ranges from simple to pretty weird How can we pass (give) the information stored inside the variables x and y to the method ToolBox.min
5
Parameter passing mechanisms Definition: Parameter passing mechanism = agreement between the calling method and the called method on how a parameter is passed between them
6
Parameter passing mechanisms (cont.) Important note: Both the calling method and the called method must agree to use the same passing mechanism (or else, the information will be passed incorrectly)
7
Parameter passing mechanisms (cont.) Most commonly used parameter passing mechanisms: Pass-by-value The calling method passes the information stored inside a variable by passing (= copying) the value contained inside a variable into the parameter variable.
8
Parameter passing mechanisms (cont.) This is the most obvious way to pass information... Example: if you want to give you phone number of your home to someone, you make a copy of the information (in the parameter variable)
9
Parameter passing mechanisms (cont.) Pass-by-reference The calling method passes the information stored inside a variable by passing (= copying) the address (location) of a variable into the parameter variable.
10
Parameter passing mechanisms (cont.) This is a less obvious but more powerful way to pass information... Example: if you want to give you phone number of your home to someone, you make a copy of the address of your home (in the parameter variable) He/she can find the phone number by visiting that address !!!
11
Parameter passing mechanisms (cont.) Terminology: A reference in Computer Science is the location (or address) (of a variable or a method)
12
Terminology: formal parameters and actual parameters Definitions: Formal parameter = a parameter variable Actual parameter = a variable whose value is to be passed to some formal parameter
13
Terminology: formal parameters and actual parameters (cont.) Illustrated example:
14
Terminology: formal parameters and actual parameters (cont.) Explanation: The parameter variables a and b in the definition of the ToolBox.min method are formal parameters The variables x and y used in the method invocation ToolBox.min(x, y) are actual parameters
15
The Pass-by-value mechanism - the agreement Recall: Parameter passing mechanism = agreement between the calling method and the called method on how a parameter is passed between them
16
The Pass-by-value mechanism - the agreement (cont.) The agreement used in the Pass-by-value mechanism: For the calling method: The calling method creates the parameter variables for the called method,.... and The calling method copies the value of the actual parameter into the formal parameter
17
The Pass-by-value mechanism - the agreement (cont.) For the called method: The called method obtains the information directly from the parameter variables
18
The Pass-by-value mechanism - an example Example program:
19
The Pass-by-value mechanism - an example (cont.) When main starts running, it will first create its local variables:
20
The Pass-by-value mechanism - an example (cont.) When execution reaches the method call ToolBox.min(x,y), the Pass-by-value mechanism first creates the parameter variables:
21
The Pass-by-value mechanism - an example (cont.) Then the Pass-by-value mechanism copies the value of the actual parameter to the corresponding formal parameter:
22
The Pass-by-value mechanism - an example (cont.) The method invocation mechanism is completed as usually with the following steps: Save the return address on the stack:
23
The Pass-by-value mechanism - an example (cont.) Jump to the called method:
24
The Pass-by-value mechanism - an example (cont.) When the min method executes, it will create its local variable m:
25
The Pass-by-value mechanism - an example (cont.) Notice how the called method uses the parameter variables: When the called method uses a parameter variable, the information is obtained directly from the parameter variable:
26
A quiz on the Pass-by-value mechanism Consider the following program:
27
A quiz on the Pass-by-value mechanism (cont.) Questions: What value is printed by the statement System.out.println(x); ? What value is printed by the statement System.out.println(y); ? What value is printed by the statement System.out.println(r); ?
28
A quiz on the Pass-by-value mechanism (cont.) Example Program: (Demo above code) –Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/P rogs/pass-by-value/quiz/MyProgram.java -Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/P rogs/pass-by-value/quiz/ToolBox.java How to run the program: Right click on links and save in a scratch directory To compile: javac MyProgram.java To run: java MyProgram
29
A quiz on the Pass-by-value mechanism (cont.) Output of the program: Did you understand why the update statements "a = a + 1" and "b = b + 2" did not update the actual parameters x and y ??? 1.0 (the value in x is UNCHANGEDD !) 4.0 (the value in y is UNCHANGEDD !) 8.0 (= 2.0 + 6.0)
30
The quiz explained Notice the similarities between the ToolBox.min and the ToolBox.fun methods: public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; } else { m = b; } return(m); } public static double fun ( double a, double b ) { double m = 0; a = a + 1; b = b + 2; m = a + b; return(m); }
31
The quiz explained (cont.) Both methods have 2 parameter variables and 1 local variable I have constructed the quiz in such a way that I can re-use the diagrams from the Pass-by-value example above.
32
The quiz explained (cont.) So according to the Pass-by-value example above, when the ToolBox.min method starts running, the following variables have been created on the System Stack:
33
The quiz explained (cont.) Notice that: are different variables (they occupy different memory cells !) The local variables x and y in the main method and The parameter variables a and b in the fun method
34
The quiz explained (cont.) The assignment statements: will change the values of the parameter variables: a = a + 1; b = b + 2;
35
The quiz explained (cont.)
36
Notice that: The values in the actual parameters (x and y) are unchanged !!!
37
The quiz explained (cont.) That's why the statements System.out.println(x); ---> prints 1.0 System.out.println(y); ---> prints 4.0
38
The quiz with an additional twist... Now, consider the following program:
39
The quiz with an additional twist... (cont.) We use the same names for actual and formal parameters !!! Questions: What value is printed by the statement System.out.println(a); ? What value is printed by the statement System.out.println(b); ? What value is printed by the statement System.out.println(r); ?
40
The quiz with an additional twist... (cont.) Example Program: (Demo above code) –Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/P rogs/pass-by-value/quiz2/MyProgram.java -Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/P rogs/pass-by-value/quiz2/ToolBox.java How to run the program: Right click on links and save in a scratch directory To compile: javac MyProgram.java To run: java MyProgram
41
The quiz with an additional twist... (cont.) Output of the program: Did you understand why the update statements "a = a + 1" and "b = b + 2" (that updates the formal parameters) did not update the actual parameters a and b ??? 1.0 (the value in x is UNCHANGEDD !) 4.0 (the value in y is UNCHANGEDD !) 8.0 (= 2.0 + 6.0)
42
The quiz explained Recall that: Different method scopes are always disjoint scopes You can define different variables with the same name in disjoint scopes (See: http://www.mathcs.emory.edu/~cheung/Courses/17 0/Syllabus/08/scope.html#disjoint)
43
The quiz explained (cont.) In other words: are different variables The local variables named a and b defined inside the main method and The parameter variables named a and b defined inside the fun method
44
The quiz explained (cont.) The following diagram shows the fact that there are 2 different variables with the same name created created on the System Stack:
45
The quiz explained (cont.) Notice that: are different variables --- it's possible because of the scopes are non-overlapping (furthermore, they use different memory cells !) The blue colored variables named a and b are inside the scope of the main method and The magenta colored variables named a and b are inside the scope of the fun method
46
The quiz explained (cont.) The assignment statements: will change the values of the parameter variables: a = a + 1; b = b + 2;
47
The quiz explained (cont.)
48
Notice that: The values in the actual parameters (a and b) are unchanged !!!
49
The quiz explained (cont.) That's why the statements System.out.println(a); ---> prints 1.0 System.out.println(b); ---> prints 4.0
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.