29
Parameter Passing 7 September 2015 OSU CSE 1

Parameter Passing - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/07.Parameter-Passing.pdf · Connecting Caller and Callee • When you call a method, how

Embed Size (px)

Citation preview

Parameter Passing

7 September 2015 OSU CSE 1

Connecting Caller and Callee

• When you call a method, how are the arguments connected to the formal parameters?

• When the called method body returns, how are results communicated back to the code that called the method?

7 September 2015 OSU CSE 2

Example: GCD

• Suppose we have a static method gcd that computes and returns the greatest common divisor (GCD) of two ints:

public static int gcd(int i, int j) {...

}

• For example:– GCD(24,80) = 8– GCD(24,24) = 24

7 September 2015 OSU CSE 3

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 4

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

This is the method gcd that is being called; i and j are its

formal parameters.

7 September 2015 OSU CSE 5

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

This is a fragment of the calling program; a and b are the

arguments to this call of gcd.

7 September 2015 OSU CSE 6

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

Suppose the solid red arrow indicates where program flow-of-

control has taken us so far.

7 September 2015 OSU CSE 7

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 8

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 9

?a

?b

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 10

24a

80b

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 11

24a

80b

The call to gcd begins ...

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 12

24a

80b

... so the formal parameters are effectively declared ...

?i

?j

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 13

24i

80j

24a

80b

... and the argument values are copied to initialize them.

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

Execution of the calling program is “paused” at the point of the call…

7 September 2015 OSU CSE 14

24i

80j

24a

80b

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

… and control is transferred to the beginning of the method body.

7 September 2015 OSU CSE 15

24i

80j

24a

80b

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

The scope of these variables is the calling program where

they are declared.

7 September 2015 OSU CSE 16

24i

80j

24a

80b

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

The scope of these variables is the method body where

they are declared.

7 September 2015 OSU CSE 17

24i

80j

24a

80b

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 18

24i

80j

24a

80b

1k

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 19

0i

8j

24a

80b

8k

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 20

0i

8j

24a

80b

8k

The return statement immediately ends execution of method body…

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 21

0i

8j

24a

80b

8k

... so the returned value is copiedback to the calling program …

8

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 22

24a

80b

... and the method body has finished, so its variables go away.

8

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 23

24a

80b

Note that the values of the formal parameters are not copied back to

the arguments!

8

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 24

24a

80b

Execution of the calling program “resumes” in mid-statement ...

8

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 25

24a

80b

8c

... and the value that was returned by the call is assigned to c.

How Calls Work In Javapublic static int gcd(int i, int j) {int k = 1;...return k;}

...int a, b;...int c = gcd(a, b);

7 September 2015 OSU CSE 26

24a

80b

8c

Connecting Caller and Callee

• When you call a method, how are the arguments connected to the formal parameters?– The argument values are copied into the formal

parameters to initialize them• When the called method body returns, how

are results communicated back to the code that called the method?– Only the returned value is copied back to the

caller; the formal parameters are simply “lost”7 September 2015 OSU CSE 27

Names for This?

• Parameter-passing mechanism of Java:– May be termed call-by-copying because

argument values are copied into formal parameters

– May be termed call-by-value because argument values are copied into formal parameters

• There are other ways it might have been done (and is done in some languages)

7 September 2015 OSU CSE 28

Tracing Over a Call

7 September 2015 OSU CSE 29

Code State

a = 24b = 80

int c = gcd(a, b);

a = 24b = 80c = 8