r = ToolBox.min( 1.0, 4.0 ); |
Specifically, we will study the pass-by-value mechanism
|
There are quite a few ways to allow (enable) you to accomplish this "passing"
The possible answers ranges from simple to pretty weird
|
Illustrated example:
Explanation:
|
|
|
|
In the next webpage, we will study the pass-by-reference mechanism.
|
|
|
|
Questions:
|
How to run the program:
|
Output of the program:
1.0 (the value in x is UNCHANGED !) 4.0 (the value in y is UNCHANGED !) 8.0 (= 2.0 + 6.0) |
Did you understand why the update statements "a = a + 1" and "b = b + 2" did not update the actual parameters x and y ???
public static double min ( double a, double b ) { double m = 0; if ( a < 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); } |
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.
|
are different variables (they occupy different memory cells !)
a = a + 1; b = b + 2; |
will change the values of the parameter variables:
|
That's why the statements
System.out.println(x); ---> prints 1.0 System.out.println(y); ---> prints 4.0 |
We use the same names for actual and formal parameters !!!
Questions:
|
How to run the program:
|
Output of the program:
1.0 (the value in a is UNCHANGED !) 4.0 (the value in b is UNCHANGED !) 8.0 (= 2.0 + 6.0) |
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 ???
|
In other words:
|
are different variables
|
are different variables --- it's possible because of the scopes are non-overlapping (furthermore, they use different memory cells !)
a = a + 1; b = b + 2; |
will change the values of the parameter variables:
|
That's why the statements
System.out.println(a); ---> prints 1.0 System.out.println(b); ---> prints 4.0 |