parameter passing mechanism pass by reference
Download
Skip this Video
Download Presentation
Parameter passing mechanism: pass-by-reference

Loading in 2 Seconds...

play fullscreen
1 / 34

Parameter passing mechanism: pass-by-reference - PowerPoint PPT Presentation


  • 86 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Parameter passing mechanism: pass-by-reference' - isaac-mercer


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the pass by reference mechanism the agreement
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 Pass-by-reference mechanism - the agreement (cont.)
  • The agreement used in the Pass-by-reference mechanism:

For the calling method:

  • The calling methodcreates the parameter variables for the called method, .... and
  • The calling methodcopies the reference (= address) of the actual parameter into the formal parameter
the pass by reference mechanism the agreement cont1
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
The Pass-by-reference mechanism is not available in Java
  • Fact:
  • The Java programming languageonly provides the pass-by-value mechanism
  • It does not provide the pass-by-reference mechanism
the pass by reference mechanism is not available in java cont
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 cont1
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 cont
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 cont1
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 cont3
The Pass-by-reference mechanism - an hypothetical example (cont.)
  • When execution reaches the method call ToolBox.min(x,y), the Pass-by-reference mechanismfirst creates the parameter variables:
the pass by reference mechanism an hypothetical example cont4
The Pass-by-reference mechanism - an hypothetical example (cont.)
  • Then the Pass-by-reference mechanismcopies the reference of the actual parameter to the corresponding formal parameter:
the pass by reference mechanism an hypothetical example cont5
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 cont6
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 cont8
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 cont9
The Pass-by-reference mechanism - an hypothetical example (cont.)
  • How the called method uses the parameter variables:
  • To access the informationpassed 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 cont10
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
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
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 cont1
A quiz on the Pass-by-reference mechanism (cont.)
  • Answer:

Did you understandwhy 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)

the quiz explained
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
The quiz explained (cont.)
  • Notice that:
  • The parameter variables a and b in the fun methodreference 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 cont1
The quiz explained (cont.)
  • The assignment statement:

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

a = a + 1;

the quiz explained cont3
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;

the quiz explained cont4
The quiz explained (cont.)
  • Notice that:
  • The values in the actual parameters (x and y) are unchanged !!!
the quiz explained cont5
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
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
illustrating the pass by reference mechanism using c cont
Illustrating the pass-by-reference mechanism using C++ (cont.)

// 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;

}

// 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;

}

  • C++ programs of the above examples:
illustrating the pass by reference mechanism using c cont2
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

illustrating the pass by reference mechanism using c cont3
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  
ad