Parameter Passing 7 January 2019 OSU CSE 1 Connecting Caller and - - PowerPoint PPT Presentation

parameter passing
SMART_READER_LITE
LIVE PREVIEW

Parameter Passing 7 January 2019 OSU CSE 1 Connecting Caller and - - PowerPoint PPT Presentation

Parameter Passing 7 January 2019 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


slide-1
SLIDE 1

Parameter Passing

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

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 January 2019 OSU CSE 2

slide-3
SLIDE 3

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 January 2019 OSU CSE 3

slide-4
SLIDE 4

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 4

slide-5
SLIDE 5

How Calls Work In Java

public 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 January 2019 OSU CSE 5

slide-6
SLIDE 6

How Calls Work In Java

public 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 January 2019 OSU CSE 6

slide-7
SLIDE 7

How Calls Work In Java

public 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 January 2019 OSU CSE 7

slide-8
SLIDE 8

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 8

slide-9
SLIDE 9

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 9

? a ? b

slide-10
SLIDE 10

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 10

24 a 80 b

slide-11
SLIDE 11

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 11

24 a 80 b

The call to gcd begins ...

slide-12
SLIDE 12

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 12

24 a 80 b

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

? i ? j

slide-13
SLIDE 13

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 13

24 i 80 j 24 a 80 b

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

slide-14
SLIDE 14

How Calls Work In Java

public 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 January 2019 OSU CSE 14

24 i 80 j 24 a 80 b

slide-15
SLIDE 15

How Calls Work In Java

public 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 January 2019 OSU CSE 15

24 i 80 j 24 a 80 b

slide-16
SLIDE 16

How Calls Work In Java

public 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 January 2019 OSU CSE 16

24 i 80 j 24 a 80 b

slide-17
SLIDE 17

How Calls Work In Java

public 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 January 2019 OSU CSE 17

24 i 80 j 24 a 80 b

slide-18
SLIDE 18

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 18

24 i 80 j 24 a 80 b 1 k

slide-19
SLIDE 19

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 19

i 8 j 24 a 80 b 8 k

slide-20
SLIDE 20

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 20

i 8 j 24 a 80 b 8 k

The return statement immediately ends execution of method body…

slide-21
SLIDE 21

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 21

i 8 j 24 a 80 b 8 k

... so the returned value is copied back to the calling program … 8

slide-22
SLIDE 22

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 22

24 a 80 b

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

slide-23
SLIDE 23

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 23

24 a 80 b

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

slide-24
SLIDE 24

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 24

24 a 80 b

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

slide-25
SLIDE 25

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 25

24 a 80 b 8 c

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

slide-26
SLIDE 26

How Calls Work In Java

public static int gcd(int i, int j) { int k = 1; ... return k; } ... int a, b; ... int c = gcd(a, b);

7 January 2019 OSU CSE 26

24 a 80 b 8 c

slide-27
SLIDE 27

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 January 2019 OSU CSE 27

slide-28
SLIDE 28

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 January 2019 OSU CSE 28

slide-29
SLIDE 29

Tracing Over a Call

7 January 2019 OSU CSE 29

Code State

a = 24 b = 80 int c = gcd(a, b); a = 24 b = 80 c = 8