Download pass-by-reference - Emory`s Math Department

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Monitor (synchronization) wikipedia , lookup

Java performance wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

C syntax wikipedia , lookup

Rental harmony wikipedia , lookup

Simplex algorithm wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

One-pass compiler wikipedia , lookup

Subroutine wikipedia , lookup

False position method wikipedia , lookup

C Sharp syntax wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Parameter (computer programming) wikipedia , lookup

Transcript
Parameter passing mechanism:
pass-by-reference
The Pass-by-reference mechanism - the
agreement
• Recall:
• Parameter passing mechanism = agreement between
the calling method and the called method on how a
parameter is passed between them
The Pass-by-reference mechanism - the
agreement (cont.)
• The agreement used in the Pass-by-reference mechanism:
For the calling method:
• The calling method creates the
parameter variables for the called method,
.... and
• The calling method copies the reference
(= address) of the actual parameter into
the formal parameter
The Pass-by-reference mechanism - the
agreement (cont.)
For the called method:
• First, the called method uses the
reference (= address) stored in the
parameter variables to locate the actual
parameter
• Once the actual parameter have been
located, the called method can
subsequently obtain the information from
the actual variables
The Pass-by-reference mechanism is not
available in Java
• Fact:
• The Java programming language only provides the passby-value mechanism
It does not provide the pass-by-reference mechanism
The Pass-by-reference mechanism is not
available in Java (cont.)
• Nevertheless:
• It is important to know the pass-by-reference
mechanism
We are --- after all --- trying to teach you Computer
Science, and not Java programming.
The Pass-by-reference mechanism is not
available in Java (cont.)
• I will now show you an example of the Pass-by-reference
mechanism
The programming language that I will use is Java -because you are familiar with Java.
But realize that:
• The example is purely hypothetical --- because ....
Java does not support Pass-by-reference
The Pass-by-reference mechanism - an
hypothetical example
• Example (hypothetical) program:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• When main starts running, it will first create its local
variables:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• We need to know the address of each variable to expose
the details of the pass-by-reference mechanism.
Let us assume that the addresses of the variables are as
follows:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• When execution reaches the method call
ToolBox.min(x,y), the Pass-by-reference mechanism first
creates the parameter variables:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• Then the Pass-by-reference mechanism copies the
reference of the actual parameter to the corresponding
formal parameter:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• Notice that:
• The parameter variables (a and b) contains the
addresses of the actual parameters
• The parameter variables (a and b) call tell us where to
find the information that we need !!!
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• The method invocation mechanism is completed as usually
with the following steps:
• Save the return address on the stack:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• Jump to the called method:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• When the min method executes, it will create its local
variable m:
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• How the called method uses the parameter variables:
• To access the information passed to the method, we
first use the reference information stored in the
parameter variables to locate the actual parameters :
The Pass-by-reference mechanism - an
hypothetical example (cont.)
• Then it access the information using the a actual parameter :
A quiz on the Pass-by-reference mechanism
• Consider the following program (again: hypothetical
because Java does not support pass-by-reference):
A quiz on the Pass-by-reference mechanism
(cont.)
• Questions:
• What reference is printed by the statement
System.out.println(x); ?
• What reference is printed by the statement
System.out.println(y); ?
• What reference is printed by the statement
System.out.println(r); ?
A quiz on the Pass-by-reference mechanism
(cont.)
• Answer:
2.0
(the value in x is CHANGEDD !!!!!!)
6.0
(the value in y is CHANGEDD !!!!!!)
8.0
(= 2.0 + 6.0)
Did you understand why the update statements "a = a + 1"
and "b = b + 2" change the actual parameters x and y ???
The quiz explained
• According to the Pass-by-reference example above, when
the ToolBox.min method starts running, the following
variables have been created on the System Stack:
The quiz explained (cont.)
• Notice that:
• The parameter variables a and b in the fun method
reference to the local variables x and y, respectively,
inside the main method
• Therefore, we will:
• use the local variable x in an operation that
involves the parameter variable a
• use the local variable y in an operation that
involves the parameter variable b
The quiz explained (cont.)
• The assignment statement:
a = a + 1;
will change the values of the actual parameter variable x
through the following mechanism:
The quiz explained (cont.)
The quiz explained (cont.)
• Similarly, the assignment statement:
b = b + 1;
will change the values of the actual parameter variable y
to 6.0 (not shown)
The quiz explained (cont.)
• Notice that:
• The values in the actual parameters (x and y) are
unchanged !!!
The quiz explained (cont.)
• That's why, if we could use the pass-by-reference
mechanism, the statements would print:
System.out.println(x); ---> prints 2.0
System.out.println(y); ---> prints 6.0
Illustrating the pass-by-reference
mechanism using C++
• I can should you the difference between
• Pass-by-value, and
• Pass-by-reference
using the C++ language (because C++ provide both
mechanisms)
Illustrating the pass-by-reference
mechanism using C++ (cont.)
• C++ programs of the above examples:
// No & means: pass-by-value
double fun ( double a, double b )
{
double m;
// With & means: pass-by-reference
double fun ( double & a, double & b )
{
double m;
a = a + 1;
b = b + 2;
m = a + b;
a = a + 1;
b = b + 2;
m = a + b;
return(m);
return(m);
}
}
int main(int argc, char **argv)
{
double x = 1.0, y = 4.0;;
double r;
int main(int argc, char **argv)
{
double x = 1.0, y = 4.0;;
double r;
}
r = fun( x, y );
r = fun( x, y );
cout << x << endl;
cout << y << endl;
cout << r << endl;
cout << x << endl;
cout << y << endl;
cout << r << endl;
}
Illustrating the pass-by-reference
mechanism using C++ (cont.)
Output:
Output:
1
2
4
6
8
8
Illustrating the pass-by-reference
mechanism using C++ (cont.)
• Example Program: (Demo above code)
– C++ Prog file using pass-by-value:
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/P
rogs/pass-by-ref/pass-by-value.C
– C++ Prog file using pass-by-reference:
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/P
rogs/pass-by-ref/pass-by-ref.C
Illustrating the pass-by-reference
mechanism using C++ (cont.)
• How to run the program:
• Right click on links and save in a scratch directory
• To compile:
• CC -o pass-by-value pass-by-value.C
• CC -o pass-by-ref pass-by-ref.C
• To run:
• pass-by-value
• pass-by-ref