parameter passing mechanism: pass-by-value. introduction in the last webpage, we discussed how to...

49
Parameter passing mechanism: pass-by-value

Upload: leslie-dalton

Post on 01-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Parameter passing mechanism: pass-by-value

Introduction

• In the last webpage, we discussed how to pass information to a method

• I have kept it (deliberately) simple by using constant values as parameters:

• In this webpage, we will discuss how to pass information stored in variables to a method.

Specifically, we will study the pass-by-value mechanism

r = ToolBox.min( 1.0, 4.0 );

Example: passing information stored in variables

• Consider the following program:

Example: passing information stored in variables (cont.)

• Question to ponder:

There are quite a few ways to allow (enable) you to

accomplish this "passing"

The possible answers ranges from simple to pretty weird

• How can we pass (give) the information stored inside the variables x and y to the method ToolBox.min

Parameter passing mechanisms

• Definition:

• Parameter passing mechanism = agreement between the calling method and the called method on how a parameter is passed between them

Parameter passing mechanisms (cont.)

• Important note:

• Both the calling method and the called method must agree to use the same passing mechanism (or else, the information will be passed incorrectly)

Parameter passing mechanisms (cont.)

• Most commonly used parameter passing mechanisms:

Pass-by-value

The calling method passes the information stored inside a variable by passing (= copying) the value contained inside a variable into the parameter variable.

Parameter passing mechanisms (cont.)

This is the most obvious way to pass information...

Example:

• if you want to give you phone number of your home to someone, you make a copy of the information (in the parameter variable)

Parameter passing mechanisms (cont.)

Pass-by-reference

• The calling method passes the information stored inside a variable by passing (= copying) the address (location) of a variable into the parameter variable.

Parameter passing mechanisms (cont.)

This is a less obvious but more powerful way to pass information...

Example:

• if you want to give you phone number of your home to someone, you make a copy of the address of your home (in the parameter variable)

He/she can find the phone number by visiting that address !!!

Parameter passing mechanisms (cont.)

• Terminology:

• A reference in Computer Science is the location (or address) (of a variable or a method)

Terminology: formal parameters and actual parameters

• Definitions:

• Formal parameter = a parameter variable

• Actual parameter = a variable whose value is to be passed to some formal parameter

Terminology: formal parameters and actual parameters (cont.)

• Illustrated example:

Terminology: formal parameters and actual parameters (cont.)

• Explanation:

• The parameter variables a and b in the definition of the ToolBox.min method are formal parameters

• The variables x and y used in the method invocation ToolBox.min(x, y) are actual parameters

The Pass-by-value 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-value mechanism - the agreement (cont.)

• The agreement used in the Pass-by-value mechanism:

• For the calling method:

• The calling method creates the parameter variables for the called method, .... and

• The calling method copies the value of the actual parameter into the formal parameter

The Pass-by-value mechanism - the agreement (cont.)

• For the called method:

• The called method obtains the information directly from the parameter variables

The Pass-by-value mechanism - an example

• Example program:

The Pass-by-value mechanism - an example (cont.)

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

The Pass-by-value mechanism - an example (cont.)

• When execution reaches the method call ToolBox.min(x,y), the Pass-by-value mechanism first creates the parameter variables:

The Pass-by-value mechanism - an example (cont.)

• Then the Pass-by-value mechanism copies the value of the actual parameter to the corresponding formal parameter:

The Pass-by-value mechanism - an example (cont.)

• The method invocation mechanism is completed as usually with the following steps:

• Save the return address on the stack:

The Pass-by-value mechanism - an example (cont.)

• Jump to the called method:

The Pass-by-value mechanism - an example (cont.)

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

The Pass-by-value mechanism - an example (cont.)

• Notice how the called method uses the parameter variables:

• When the called method uses a parameter variable, the information is obtained directly from the parameter variable:

A quiz on the Pass-by-value mechanism

• Consider the following program:

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

• Questions:

• What value is printed by the statement System.out.println(x); ?

• What value is printed by the statement System.out.println(y); ?

• What value is printed by the statement System.out.println(r); ?

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

• Example Program: (Demo above code)         – Prog file:

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/Progs/pass-by-value/quiz/MyProgram.java

- Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/Progs/pass-by-value/quiz/ToolBox.java

• How to run the program:

• Right click on links and save in a scratch directory

• To compile:   javac MyProgram.java

• To run:          java MyProgram

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

• Output of the program:

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

1.0 (the value in x is UNCHANGEDD !)

4.0 (the value in y is UNCHANGEDD !)

8.0 (= 2.0 + 6.0)

The quiz explained

• Notice the similarities between the ToolBox.min and the ToolBox.fun methods:

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

The quiz explained (cont.)

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

The quiz explained (cont.)

• So according to the Pass-by-value 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:

are different variables (they occupy different memory cells !)

• The local variables x and y in the main method      and      

• The parameter variables a and b in the fun method

The quiz explained (cont.)

• The assignment statements:

will change the values of the parameter variables:

a = a + 1;b = b + 2;

The quiz explained (cont.)

The quiz explained (cont.)

• Notice that:

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

The quiz explained (cont.)

• That's why the statements

System.out.println(x); ---> prints 1.0 System.out.println(y); ---> prints 4.0

The quiz with an additional twist...

• Now, consider the following program:

The quiz with an additional twist... (cont.)

• We use the same names for actual and formal parameters !!!

• Questions:

• What value is printed by the statement System.out.println(a); ?

• What value is printed by the statement System.out.println(b); ?

• What value is printed by the statement System.out.println(r); ?

The quiz with an additional twist... (cont.)

• Example Program: (Demo above code)         – Prog file:

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/Progs/pass-by-value/quiz2/MyProgram.java

- Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/Progs/pass-by-value/quiz2/ToolBox.java

• How to run the program:

• Right click on links and save in a scratch directory

• To compile:   javac MyProgram.java

• To run:          java MyProgram

The quiz with an additional twist... (cont.)

• Output of the program:

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

1.0 (the value in x is UNCHANGEDD !)

4.0 (the value in y is UNCHANGEDD !)

8.0 (= 2.0 + 6.0)

The quiz explained

• Recall that:

• Different method scopes are always disjoint scopes

• You can define different variables with the same name in disjoint scopes (See: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/08/scope.html#disjoint)

The quiz explained (cont.)

• In other words:

are different variables

• The local variables named a and b defined inside the main method      and      

• The parameter variables named a and b defined inside the fun method

The quiz explained (cont.)

• The following diagram shows the fact that there are 2 different variables with the same name created created on the System Stack:

The quiz explained (cont.)

• Notice that:

are different variables --- it's possible because of the scopes are non-overlapping (furthermore, they use different memory cells !)

• The blue colored variables named a and b are inside the scope of the main method      and      

• The magenta colored variables named a and b are inside the scope of the fun method

The quiz explained (cont.)

• The assignment statements:

will change the values of the parameter variables:

a = a + 1;

b = b + 2;

The quiz explained (cont.)

The quiz explained (cont.)

• Notice that:

• The values in the actual parameters (a and b) are unchanged !!!

The quiz explained (cont.)

• That's why the statements

System.out.println(a); ---> prints 1.0 System.out.println(b); ---> prints 4.0