1 / 34

Parameter passing mechanism: pass-by-reference

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.

denton-moss
Download Presentation

Parameter passing mechanism: pass-by-reference

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parameter passing mechanism: pass-by-reference

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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.

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

  8. The Pass-by-reference mechanism - an hypothetical example • Example (hypothetical) program:

  9. The Pass-by-reference mechanism - an hypothetical example (cont.) • When main starts running, it will first create its local variables:

  10. 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:

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

  12. 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:

  13. 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:

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

  15. 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:

  16. The Pass-by-reference mechanism - an hypothetical example (cont.) • Jump to the called method:

  17. The Pass-by-reference mechanism - an hypothetical example (cont.) • When the min method executes, it will create its local variable m:

  18. 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 :

  19. The Pass-by-reference mechanism - an hypothetical example (cont.) • Then it access the information using the a actual parameter :

  20. A quiz on the Pass-by-reference mechanism • Consider the following program (again: hypothetical because Java does not support pass-by-reference):

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

  22. 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)

  23. 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:

  24. 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

  25. The quiz explained (cont.) • The assignment statement: will change the values of the actual parameter variable x through the following mechanism: a = a + 1;

  26. The quiz explained (cont.)

  27. 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;

  28. The quiz explained (cont.) • Notice that: • The values in the actual parameters (x and y) are unchanged !!!

  29. 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

  30. 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

  31. 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:

  32. Illustrating the pass-by-reference mechanism using C++ (cont.) Output: 1 4 8 Output: 2 6 8

  33. 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

  34. 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  

More Related