+ All Categories
Home > Documents > Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement...

Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement...

Date post: 01-Apr-2015
Category:
Upload: jordan-taft
View: 238 times
Download: 0 times
Share this document with a friend
Popular Tags:
34
Parameter passing mechanism: pass-by- reference
Transcript
Page 1: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

Parameter passing mechanism: pass-by-reference

Page 2: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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

Page 3: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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

Page 4: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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

Page 5: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The Pass-by-reference mechanism is not available in Java

• Fact:

• The Java programming language only provides the pass-by-value mechanism

It does not provide the pass-by-reference mechanism

Page 6: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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.

Page 7: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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

Page 8: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The Pass-by-reference mechanism - an hypothetical example

• Example (hypothetical) program:

Page 9: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The Pass-by-reference mechanism - an hypothetical example (cont.)

• When main starts running, it will first create its local variables:

Page 10: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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:

Page 11: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The Pass-by-reference mechanism - an hypothetical example (cont.)

Page 12: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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:

Page 13: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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:

Page 14: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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 !!!

Page 15: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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:

Page 16: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The Pass-by-reference mechanism - an hypothetical example (cont.)

• Jump to the called method:

Page 17: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The Pass-by-reference mechanism - an hypothetical example (cont.)

• When the min method executes, it will create its local variable m:

Page 18: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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 :

Page 19: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The Pass-by-reference mechanism - an hypothetical example (cont.)

• Then it access the information using the a actual parameter :

Page 20: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

A quiz on the Pass-by-reference mechanism

• Consider the following program (again: hypothetical because Java does not support pass-by-reference):

Page 21: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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); ?

Page 22: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

A quiz on the Pass-by-reference mechanism (cont.)

• Answer:

Did you understand why the update statements "a = a + 1" and "b = b + 2" change the actual parameters x and y ???

2.0 (the value in x is CHANGEDD !!!!!!)

6.0 (the value in y is CHANGEDD !!!!!!)

8.0 (= 2.0 + 6.0)

Page 23: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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:

Page 24: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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

Page 25: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The quiz explained (cont.)

• The assignment statement:

will change the values of the actual parameter variable x through the following mechanism:

a = a + 1;

Page 26: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The quiz explained (cont.)

Page 27: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The quiz explained (cont.)

• Similarly, the assignment statement:

will change the values of the actual parameter variable y to 6.0 (not shown)

b = b + 1;

Page 28: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

The quiz explained (cont.)

• Notice that:

• The values in the actual parameters (x and y) are unchanged !!!

Page 29: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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

Page 30: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

Illustrating the pass-by-reference mechanism using C++

• I can should you the difference between

using the C++ language (because C++ provide both mechanisms)

• Pass-by-value, and

• Pass-by-reference

Page 31: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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; a = a + 1; b = b + 2; m = a + b; return(m); } int main(int argc, char **argv) { double x = 1.0, y = 4.0;; double r; r = fun( x, y ); cout << x << endl; cout << y << endl; cout << r << endl; }

// With & means: pass-by-reference double fun ( double & a, double & b ) { double m; a = a + 1; b = b + 2; m = a + b; return(m); } int main(int argc, char **argv) { double x = 1.0, y = 4.0;; double r; r = fun( x, y ); cout << x << endl; cout << y << endl; cout << r << endl; }

Page 32: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

Illustrating the pass-by-reference mechanism using C++ (cont.)

Output:

1

4

8

Output:

2

6

8

Page 33: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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/Progs/pass-by-ref/pass-by-value.C

– C++ Prog file using pass-by-reference:

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/Progs/pass-by-ref/pass-by-ref.C

Page 34: Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.

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  


Recommended