6. Parameter Passing Parameter Passing CS 381 Spring 2016 Example - - PowerPoint PPT Presentation

6 parameter passing
SMART_READER_LITE
LIVE PREVIEW

6. Parameter Passing Parameter Passing CS 381 Spring 2016 Example - - PowerPoint PPT Presentation

CS 381 Spring 2016 6. Parameter Passing Parameter Passing CS 381 Spring 2016 Example (Formal) Parameter void f(int x, int y) { y := x+1 }; x := 3; What is the value of z? z := 1; f(2*x,z); ... it depends on the parameter


slide-1
SLIDE 1

CS 381 • Spring 2016 Parameter Passing

  • 6. Parameter Passing
slide-2
SLIDE 2

CS 381 • Spring 2016 Parameter Passing

What is the value of z? ... it depends on the 
 parameter passing mechanism.

Example

x := 3;
 z := 1; f(2*x,z); void f(int x, int y) { y := x+1 };

(Formal) Parameter Actual Parameter, or:
 Argument

2

slide-3
SLIDE 3

CS 381 • Spring 2016 Parameter Passing

Function Definition & Use

3

… parName * … … fName(parName, …)

Interface

Function/procedure/method name ➊ Parameter names and types ➋ Access to arguments ➌

Definition Use

fName(3,x+1,y,…)

Implementation Call

Defines: Pass arguments to parameters ➊ Pass control and execute code ➋ Return values as results ➌

slide-4
SLIDE 4

CS 381 • Spring 2016 Parameter Passing

Function Call

4

… 
 a := b+1
 c := a-b fName(a,b,c,…) fName(3,x+1,x,…)

Evaluate argument expressions ➊ Create activation record with parameter names on runtime stack ➋ Store values and locations in activation record ➌ Transfer control to function and run code ➍ ➎ Pass values back to call site ➏ Remove activation record from runtime stack

[〈a: ?, b: ?, c: ?〉, ... 〈x: 5〉 ...] [... 〈x: 5〉 ...] 6 [〈a: 3, b: 6, c: x〉, ... 〈x: 5〉 ...] [〈a: 7, b: 6, c: x〉, ... 〈x: 5〉 ...] [〈a: 7, b: 6, c: x〉, ... 〈x: 1〉 ...] N/A [... 〈x: 1〉 ...]

slide-5
SLIDE 5

CS 381 • Spring 2016 Parameter Passing

Call-By-Value

5

… 
 a := b+1
 c := a-b fName(a,b,c,…) fName(3,x+1,x,…)

Evaluate argument expressions ➊ Create activation record with parameter names on runtime stack ➋ Store values and locations in activation record ➌ Transfer control to function and run code ➍ ➎ Pass values back to call site ➏ Remove activation record from runtime stack

[〈a: ?, b: ?, c: ?〉, ... 〈x: 5〉 ...] [... 〈x: 5〉 ...] 6 [〈a: 3, b: 6, c: 5〉, ... 〈x: 5〉 ...] [〈a: 7, b: 6, c: 5〉, ... 〈x: 5〉 ...] [〈a: 7, b: 6, c: 1〉, ... 〈x: 5〉 ...] [... 〈x: 5〉 ...]

slide-6
SLIDE 6

CS 381 • Spring 2016 Parameter Passing

Call-By-Value

6

  • Evaluate argument
  • Bind resulting value to parameter
  • Look up value when needed
  • Local assignments ok, but are lost

after return from function call Synopsis

p := … … := p
 fName(p,…) p

slide-7
SLIDE 7

CS 381 • Spring 2016 Parameter Passing

Exercise: Compute under CBV

1 { int z; 2 int y; 3 y := 5; 4 { int f(int x){ 5 x := x+1; 6 y := x-4; 7 x := x+1; 8 return x; 9 }; 10 z := f(y)+y; 11 }; 12 }
 [] 1 [z:?] 2 [y:?, z:?] 3 [y:5, z:?] 4 [f:{}, y:5, z:?] 10 >> 
 [x:5, f:{}, y:5, z:?] 5 [x:6, f:{}, y:5, z:?] 6 [x:6, f:{}, y:2, z:?] 7 [x:7, f:{}, y:2, z:?] 8 [res:7, x:7, f:{}, y:2, z:?] << 10 [f:{}, y:2, z:9] ...


7

slide-8
SLIDE 8

CS 381 • Spring 2016 Parameter Passing

Call-By-Reference

8

… 
 c := c-2 fName(ref c,…) fName(3,x+1,x,…)

Evaluate argument expressions ➊ Create activation record with parameter names on runtime stack ➋ Store values and locations in activation record ➌ Transfer control to function and run code ➍ ➎ Pass values back to call site ➏ Remove activation record from runtime stack

[〈c: ?〉, ... 〈x: 5〉 ...] [... 〈x: 5〉 ...] [... 〈x: 3〉 ...] [〈c: 〉, ... 〈x: 5〉 ...] [〈c: 〉, ... 〈x: 3〉 ...]

Alias, or:
 Synonym Indicating parameter passing schema Only variables can be arguments

slide-9
SLIDE 9

CS 381 • Spring 2016 Parameter Passing

p := … … := p


Call-By-Reference

9

  • Parameter p points to variable x passed as argument
  • Parameter p is just another name for x
  • Every read/write access to p acts on x
  • Only variables can be passed as arguments

Synopsis

fName(x,…) fName(ref p,…) x

slide-10
SLIDE 10

CS 381 • Spring 2016 Parameter Passing

Exercise: Compute under CBR

1 {int z; 2 int y; 3 y := 5; 4 {int f(ref int x){ 5 x := x+1; 6 y := x-4; 7 x := x+1; 8 return x; 9 }; 10 z := f(y)+y; 11 }; 12 }
 [] 1 [z:?] 2 [y:?, z:?] 3 [y:5, z:?] 4 [f:{}, y:5, z:?] 10 >> [x->y, f:{}, y:5, z:?] 5 [x->y, f:{}, y:6, z:?] 6 [x->y, f:{}, y:2, z:?] 7 [x->y, f:{}, y:3, z:?] 8 [res:3, x->y, f:{}, y:3, z:?] 
 << 10 [f:{}, y:3, z:6] ...


10

slide-11
SLIDE 11

CS 381 • Spring 2016 Parameter Passing

Call-By-Value-Result

11

… 
 c := c-2 fName(valres c,…) fName(3,x+1,x,…)

Evaluate argument expressions ➊ Create activation record with parameter names on runtime stack ➋ Store values and locations in activation record ➌ Transfer control to function and run code ➍ ➎ Pass values back to call site ➏ Remove activation record from runtime stack

[〈c: ?〉, ... 〈x: 5〉 ...] [... 〈x: 5〉 ...] [... 〈x: 3〉 ...] [〈c: 5〉, ... 〈x: 5〉 ...] [〈c: 3〉, ... 〈x: 5〉 ...]

Indicating parameter passing schema

[〈c: 3〉, ... 〈x: 3〉 ...]

Only variables can be arguments

slide-12
SLIDE 12

CS 381 • Spring 2016 Parameter Passing

p := … … := p


Call-By-Value-Result

12

  • Copy value of argument variable x to parameter p
  • Every read/write access to p acts on p
  • Copy value of parameter p back to argument variable x
  • Only variables can be passed as arguments

Synopsis

fName(x,…) fName(valres p,…) x

copy in copy out

p

slide-13
SLIDE 13

CS 381 • Spring 2016 Parameter Passing

When exactly does the copy-out step happen when returning from a function call? Before or after the function result is set?

x := f(x); int f(valres int y) { 
 y := 7;
 return 5 }

Call-By-Value-Result & Function Results

Does it matter?

13

[〈y: 0〉, 〈x: 0〉 ...] [〈x: 0〉 ...] [〈y: 7〉, 〈x: 0〉 ...] [〈res: 5, y: 7〉, 〈x: 0〉 ...] [〈x: 5〉 ...]

“before”

[〈x: 7〉 ...]

“after” Yes! defined behavior

slide-14
SLIDE 14

CS 381 • Spring 2016 Parameter Passing

\

Exercise: Compute under CBVR

1 {int z; 2 int y; 3 y := 5; 4 {int f(valres x){ 5 x := x+1; 6 y := x-4; 7 x := x+1; 8 return x; 9 }; 10 z := f(y)+y; 11 }; 12 }
 [] 1 [z:?] 2 [y:?, z:?] 3 [y:5, z:?] 4 [f:{}, y:5, z:?] 10 >> [x:5, f:{}, y:5, z:?] 5 [x:6, f:{}, y:5, z:?] 6 [x:6, f:{}, y:2, z:?] 7 [x:7, f:{}, y:2, z:?] 8 [res:7, x:7, f:{}, y:2, z:?] 9 [res:7, x:7, f:{}, y:7, z:?] 
 << 10 [f:{}, y:7, z:14] ...


14

slide-15
SLIDE 15

CS 381 • Spring 2016 Parameter Passing

\

Comparison: CBV, CBR, CBVR

1 {int z; 2 int y; 3 y := 5; 4 {int f(? x){ 5 x := x+1; 6 y := x-4; 7 x := x+1; 8 return x; 9 }; 10 z := f(y)+y; 11 }; 12 }


[x:5, f:{}, y:5, z:?] 5 [x:6, f:{}, y:5, z:?] 6 [x:6, f:{}, y:2, z:?] 7 [x:7, f:{}, y:2, z:?] 8 [res:7, x:7, f:{}, y:2, z:?] 9 [res:7, x:7, f:{}, y:7, z:?] <<

10 [f:{}, y:7, z:14] 


15 [x->y, f:{}, y:5, z:?] 5 [x->y, f:{}, y:6, z:?] 6 [x->y, f:{}, y:2, z:?] 7 [x->y, f:{}, y:3, z:?] 8 [res:3, x->y, f:{}, y:3, z:?] <<

10 [f:{}, y:3, z:6]


[x:5, f:{}, y:5, z:?] 5 [x:6, f:{}, y:5, z:?] 6 [x:6, f:{}, y:2, z:?] 7 [x:7, f:{}, y:2, z:?] 8 [res:7, x:7, f:{}, y:2, z:?] <<

10 [f:{}, y:2, z:9]


CBV CBR CBVR

slide-16
SLIDE 16

CS 381 • Spring 2016 Parameter Passing

Call-By-Name

16

z := a-2
 x := 2
 z := 2*a fName(name a,…) fName(x+1,…)

Evaluate argument expressions ➊ Create activation record with parameter names on runtime stack ➋ Store values and locations in activation record ➌ Transfer control to function and run code ➍ ➎ Pass values back to call site ➏ Remove activation record from runtime stack

[〈a: ?〉, ... 〈x: 5〉 ...] [... 〈x: 5〉 ...] [... 〈x: 2〉 ...] [〈a: x+1〉, ... 〈x: 5〉 ...] [〈z: 4, a: x+1〉, ... 〈x: 2〉 ...]

Indicating parameter passing schema

expressions

[〈z: 4, a: x+1〉, ... 〈x: 5〉 ...] [〈z: 6, a: x+1〉, ... 〈x: 2〉 ...]

local variable

slide-17
SLIDE 17

CS 381 • Spring 2016 Parameter Passing

… := … p


Call-By-Name

17

  • Bind argument expression e to parameter p
  • Every read access to p evaluates e anew
  • Imagine substituting (e) for p in the body of fName
  • Assignments to parameter are not allowed

Synopsis

fName(e,…) fName(name p,…) … := … (e)

slide-18
SLIDE 18

CS 381 • Spring 2016 Parameter Passing

Call-By-Need

18

z := a-2
 x := 2
 z := 2*a fName(need a,…) fName(x+1,…)

Evaluate argument expressions ➊ Create activation record with parameter names on runtime stack ➋ Store values and locations in activation record ➌ Transfer control to function and run code ➍ ➎ Pass values back to call site ➏ Remove activation record from runtime stack

[〈a: ?〉, ... 〈x: 5〉 ...] [... 〈x: 5〉 ...] [... 〈x: 2〉 ...] [〈a: x+1〉, ... 〈x: 5〉 ...] [〈z: 4, a: 6〉, ... 〈x: 2〉 ...]

Indicating parameter passing schema

expressions, replaced by values

[〈z: 4, a: 6〉, ... 〈x: 5〉 ...] [〈z: 12, a: 6〉, ... 〈x: 2〉 ...]

local variable

[〈z: ?, a: x+1〉, ... 〈x: 5〉 ...]

slide-19
SLIDE 19

CS 381 • Spring 2016 Parameter Passing

Call-By-Need

19

  • Similar to call-by-name, except:
  • First read access to p evaluates e to v and stores v in p
  • Subsequent accesses to p retrieve v

Synopsis

slide-20
SLIDE 20

CS 381 • Spring 2016 Parameter Passing

Call-By-... Value Reference Value-Result Name Need restriction on argument var only var only restriction on parameter no assignment no assignment what is stored in activation record value pointer value expr expr/value how to access parameter value lookup deref lookup eval eval, then lookup

  • n return from call

copy value to actual param

Comparison

20

slide-21
SLIDE 21

CS 381 • Spring 2016 Parameter Passing

Classification

21

Value Flow in only in & out CBV CBR CBVR CBN CBNeed Activation
 Record
 Content Value Pointer Expression

slide-22
SLIDE 22

CS 381 • Spring 2016 Parameter Passing

Example Languages

22

Call-by-Need

Haskell

Call-by-Value Call-by-Reference Call-by-Value-Result Call-by-Name