Title: Parameter passing mechanism: pass-by-value
1Parameter passing mechanism pass-by-value
2Introduction
- 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 )
3Example passing information stored in variables
- Consider the following program
4Example 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
5Parameter passing mechanisms
- Parameter passing mechanism agreement between
the calling method and the called method on how a
parameter is passed between them
6Parameter passing mechanisms (cont.)
- Both the calling method and the called method
must agree to use the same passing mechanism (or
else, the information will be passed incorrectly)
7Parameter 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.
8Parameter 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)
9Parameter 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.
10Parameter 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 !!!
11Parameter passing mechanisms (cont.)
- A reference in Computer Science is the location
(or address) (of a variable or a method)
12Terminology formal parameters and actual
parameters
- Formal parameter a parameter variable
- Actual parameter a variable whose value is to
be passed to some formal parameter
13Terminology formal parameters and actual
parameters (cont.)
14Terminology formal parameters and actual
parameters (cont.)
- 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
15The Pass-by-value mechanism - the agreement
- Parameter passing mechanism agreement between
the calling method and the called method on how a
parameter is passed between them
16The Pass-by-value mechanism - the agreement
(cont.)
- The agreement used in the Pass-by-value
mechanism
- 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
17The Pass-by-value mechanism - the agreement
(cont.)
- The called method obtains the information
directly from the parameter variables
18The Pass-by-value mechanism - an example
19The Pass-by-value mechanism - an example (cont.)
- When main starts running, it will first create
its local variables
20The 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
21The 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
22The 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
23The Pass-by-value mechanism - an example (cont.)
- Jump to the called method
24The Pass-by-value mechanism - an example (cont.)
- When the min method executes, it will create its
local variable m
25The 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
26A quiz on the Pass-by-value mechanism
- Consider the following program
27A quiz on the Pass-by-value mechanism (cont.)
- 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) ?
28A quiz on the Pass-by-value mechanism (cont.)
- Example Program (Demo above code) Â Â Â Â
- Prog file
- http//www.mathcs.emory.edu/cheung/Courses/170/S
yllabus/08/Progs/pass-by-value/quiz/MyProgram.java
- Prog file http//www.mathcs.emory.edu/cheung/Cou
rses/170/Syllabus/08/Progs/pass-by-value/quiz/Tool
Box.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
29A 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)
30The 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 lt 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)
31The 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.
32The 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
33The 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
34The quiz explained (cont.)
- The assignment statements
- will change the values of the parameter
variables
a a 1 b b 2
35The quiz explained (cont.)
36The quiz explained (cont.)
- The values in the actual parameters (x and y)
are unchanged !!!
37The quiz explained (cont.)
- That's why the statements
System.out.println(x) ---gt prints 1.0
System.out.println(y) ---gt prints 4.0
38The quiz with an additional twist...
- Now, consider the following program
39The 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) ?
40The quiz with an additional twist... (cont.)
- Example Program (Demo above code) Â Â Â Â
- Prog file
- http//www.mathcs.emory.edu/cheung/Courses/170/S
yllabus/08/Progs/pass-by-value/quiz2/MyProgram.jav
a - Prog file http//www.mathcs.emory.edu/cheung/Cou
rses/170/Syllabus/08/Progs/pass-by-value/quiz2/Too
lBox.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
41The 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)
42The quiz explained
- Different method scopes are always disjoint
scopes - You can define different variables with the same
name in disjoint scopes (See http//www.mathcs.em
ory.edu/cheung/Courses/170/Syllabus/08/scope.html
disjoint)
43The 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
44The 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
45The 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
46The quiz explained (cont.)
- The assignment statements
- will change the values of the parameter
variables
a a 1 b b 2
47The quiz explained (cont.)
48The quiz explained (cont.)
- The values in the actual parameters (a and b)
are unchanged !!!
49The quiz explained (cont.)
- That's why the statements
System.out.println(a) ---gt prints 1.0
System.out.println(b) ---gt prints 4.0