6 parameter passing
play

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


  1. CS 381 • Spring 2016 6. Parameter Passing Parameter Passing

  2. 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 passing mechanism . Actual Parameter, or: 
 Argument Parameter Passing 2

  3. CS 381 • Spring 2016 Function Definition & Use Defines: Function/procedure/method name ➊ Interface Parameter names and types ➋ fName(parName, …) Access to arguments ➌ … parName * … … Implementation Definition Use fName(3,x+1,y,…) Pass arguments to parameters ➊ Call Pass control and execute code ➋ Return values as results ➌ Parameter Passing 3

  4. CS 381 • Spring 2016 Function Call fName(3,x+1,x,…) 6 Evaluate argument expressions [... 〈 x : 5 〉 ... ] ➊ [ 〈 a : ? , b : ? , c : ? 〉 , ... 〈 x : 5 〉 ... ] Create activation record with parameter names on runtime stack ➋ [ 〈 a : 3 , b : 6 , c : x 〉 , ... 〈 x : 5 〉 ... ] Store values and locations in activation record ➌ Transfer control to function and run code ➍ fName(a,b,c,…) … 
 [ 〈 a : 7 , b : 6 , c : x 〉 , ... 〈 x : 5 〉 ... ] a := b+1 
 [ 〈 a : 7 , b : 6 , c : x 〉 , ... 〈 x : 1 〉 ... ] c := a-b N/A Pass values back to call site ➎ [... 〈 x : 1 〉 ... ] Remove activation record from runtime stack ➏ Parameter Passing 4

  5. CS 381 • Spring 2016 Call-By-Value fName(3,x+1,x,…) 6 [... 〈 x : 5 〉 ... ] Evaluate argument expressions ➊ [ 〈 a : ? , b : ? , c : ? 〉 , ... 〈 x : 5 〉 ... ] Create activation record with parameter names on runtime stack ➋ [ 〈 a : 3 , b : 6 , c : 5 〉 , ... 〈 x : 5 〉 ... ] Store values and locations in activation record ➌ Transfer control to function and run code ➍ fName(a,b,c,…) … 
 [ 〈 a : 7 , b : 6 , c : 5 〉 , ... 〈 x : 5 〉 ... ] a := b+1 
 c := a-b [ 〈 a : 7 , b : 6 , c : 1 〉 , ... 〈 x : 5 〉 ... ] Pass values back to call site ➎ [... 〈 x : 5 〉 ... ] Remove activation record from runtime stack ➏ Parameter Passing 5

  6. CS 381 • Spring 2016 Call-By-Value p fName(p,…) p := … … := p 
 Synopsis • Evaluate argument • Bind resulting value to parameter • Look up value when needed • Local assignments ok, but are lost after return from function call Parameter Passing 6

  7. CS 381 • Spring 2016 Exercise: Compute under CBV [] 1 { int z; 1 [z:?] 2 int y; 2 [y:?, z:?] 3 y := 5; 3 [y:5, z:?] 4 { int f(int x){ 4 [f:{}, y:5, z:?] 5 x := x+1; 10 >> 
 [x:5, f:{}, y:5, z:?] 6 y := x-4; 5 [x:6, f:{}, y:5, z:?] 7 x := x+1; 6 [x:6, f:{}, y:2, z:?] 8 return x; 7 [x:7, f:{}, y:2, z:?] 9 }; 8 [res:7, x:7, f:{}, y:2, z:?] 10 z := f(y)+y; << 11 }; 10 [f:{}, y:2, z:9] 12 } 
 ... 
 Parameter Passing 7

  8. CS 381 • Spring 2016 Call-By-Reference Only variables can be arguments fName(3,x+1,x,…) Evaluate argument expressions [... 〈 x : 5 〉 ... ] ➊ [ 〈 c : ? 〉 , ... 〈 x : 5 〉 ... ] Create activation record with parameter names on runtime stack ➋ [ 〈 c : 〉 , ... 〈 x : 5 〉 ... ] Store values and locations in activation record ➌ Transfer control to function and run code ➍ Alias, or: 
 Synonym fName(ref c,…) Indicating … 
 parameter c := c-2 [ 〈 c : 〉 , ... 〈 x : 3 〉 ... ] passing schema Pass values back to call site ➎ [... 〈 x : 3 〉 ... ] Remove activation record from runtime stack ➏ Parameter Passing 8

  9. CS 381 • Spring 2016 Call-By-Reference x fName(x,…) fName(ref p,…) p := … … := p 
 Synopsis • 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 Parameter Passing 9

  10. CS 381 • Spring 2016 Exercise: Compute under CBR [] 1 [z:?] 1 {int z; 2 [y:?, z:?] 2 int y; 3 [y:5, z:?] 3 y := 5; 4 [f:{}, y:5, z:?] 4 {int f(ref int x){ 10 >> 5 x := x+1; [x->y, f:{}, y:5, z:?] 6 y := x-4; 5 [x->y, f:{}, y:6, z:?] 7 x := x+1; 6 [x->y, f:{}, y:2, z:?] 8 return x; 7 [x->y, f:{}, y:3, z:?] 9 }; 8 [res:3, x->y, f:{}, y:3, z:?] 
 10 z := f(y)+y; << 11 }; 10 [f:{}, y:3, z:6] 12 } 
 ... 
 Parameter Passing 10

  11. CS 381 • Spring 2016 Call-By-Value-Result Only variables can be arguments fName(3,x+1,x,…) [... 〈 x : 5 〉 ... ] Evaluate argument expressions ➊ [ 〈 c : ? 〉 , ... 〈 x : 5 〉 ... ] Create activation record with parameter names on runtime stack ➋ [ 〈 c : 5 〉 , ... 〈 x : 5 〉 ... ] Store values and locations in activation record ➌ Transfer control to function and run code ➍ fName(valres c,…) Indicating … 
 parameter c := c-2 [ 〈 c : 3 〉 , ... 〈 x : 5 〉 ... ] passing schema [ 〈 c : 3 〉 , ... 〈 x : 3 〉 ... ] Pass values back to call site ➎ [... 〈 x : 3 〉 ... ] Remove activation record from runtime stack ➏ Parameter Passing 11

  12. CS 381 • Spring 2016 Call-By-Value-Result x fName(x,…) copy out copy in p fName(valres p,…) p := … … := p 
 Synopsis • 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 Parameter Passing 12

  13. CS 381 • Spring 2016 Call-By-Value-Result & Function Results When exactly does the copy-out step happen when returning from a function call? Before or after the function result is set? [ 〈 x : 0 〉 ... ] int f(valres int y) { 
 [ 〈 y : 0 〉 , 〈 x : 0 〉 ... ] Does it matter? y := 7; 
 [ 〈 y : 7 〉 , 〈 x : 0 〉 ... ] return 5 [ 〈 res : 5 , y : 7 〉 , 〈 x : 0 〉 ... ] Yes! } “before” “after” x := f(x); [ 〈 x : 5 〉 ... ] [ 〈 x : 7 〉 ... ] defined behavior Parameter Passing 13

  14. \ CS 381 • Spring 2016 Exercise: Compute under CBVR [] 1 {int z; 1 [z:?] 2 int y; 2 [y:?, z:?] 3 y := 5; 3 [y:5, z:?] 4 {int f(valres x){ 4 [f:{}, y:5, z:?] 5 x := x+1; 10 >> 6 y := x-4; [x:5, f:{}, y:5, z:?] 7 x := x+1; 5 [x:6, f:{}, y:5, z:?] 8 return x; 6 [x:6, f:{}, y:2, z:?] 9 }; 7 [x:7, f:{}, y:2, z:?] 10 z := f(y)+y; 8 [res:7, x:7, f:{}, y:2, z:?] 11 }; 9 [res:7, x:7, f:{}, y:7, z:?] 
 << 12 } 
 10 [f:{}, y:7, z:14] ... 
 Parameter Passing 14

  15. \ CS 381 • Spring 2016 Comparison: CBV, CBR, CBVR CBV [x:5, f:{}, y:5, z:?] 5 [x:6, f:{}, y:5, z:?] 6 [x:6, f:{}, y:2, z:?] 1 {int z; 7 [x:7, f:{}, y:2, z:?] 8 [res:7, x:7, f:{}, y:2, z:?] 2 int y; << 3 y := 5; 10 [f:{}, y:2, z:9] 
 4 {int f(? x){ CBR [x->y, f:{}, y:5, z:?] 5 x := x+1; 5 [x->y, f:{}, y:6, z:?] 6 y := x-4; 6 [x->y, f:{}, y:2, z:?] 7 x := x+1; 7 [x->y, f:{}, y:3, z:?] 8 [res:3, x->y, f:{}, y:3, z:?] << 8 return x; 10 [f:{}, y:3, z:6] 
 9 }; [x:5, f:{}, y:5, z:?] 10 z := f(y)+y; 5 [x:6, f:{}, y:5, z:?] 6 [x:6, f:{}, y:2, z:?] 11 }; 7 [x:7, f:{}, y:2, z:?] 12 } 
 8 [res:7, x:7, f:{}, y:2, z:?] 9 [res:7, x:7, f:{}, y:7, z:?] << 10 [f:{}, y:7, z:14] 
 CBVR Parameter Passing 15

  16. CS 381 • Spring 2016 Call-By-Name fName(x+1,…) [... 〈 x : 5 〉 ... ] Evaluate argument expressions ➊ [ 〈 a : ? 〉 , ... 〈 x : 5 〉 ... ] Create activation record with parameter names on runtime stack ➋ expressions [ 〈 a : x+1 〉 , ... 〈 x : 5 〉 ... ] Store values and locations in activation record ➌ Transfer control to function and run code ➍ fName(name a,…) Indicating [ 〈 z : 4 , a : x+1 〉 , ... 〈 x : 5 〉 ... ] z := a-2 
 local parameter [ 〈 z : 4 , a : x+1 〉 , ... 〈 x : 2 〉 ... ] x := 2 
 variable passing schema z := 2*a [ 〈 z : 6 , a : x+1 〉 , ... 〈 x : 2 〉 ... ] Pass values back to call site ➎ [... 〈 x : 2 〉 ... ] Remove activation record from runtime stack ➏ Parameter Passing 16

  17. CS 381 • Spring 2016 Call-By-Name fName(e,…) fName(name p,…) … := … p 
 … := … (e) Synopsis • 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 Parameter Passing 17

  18. CS 381 • Spring 2016 Call-By-Need fName(x+1,…) [... 〈 x : 5 〉 ... ] Evaluate argument expressions ➊ [ 〈 a : ? 〉 , ... 〈 x : 5 〉 ... ] Create activation record with parameter names on runtime stack ➋ expressions, replaced by values [ 〈 a : x+1 〉 , ... 〈 x : 5 〉 ... ] Store values and locations in activation record ➌ Transfer control to function and run code ➍ [ 〈 z : ? , a : x+1 〉 , ... 〈 x : 5 〉 ... ] fName(need a,…) [ 〈 z : 4 , a : 6 〉 , ... 〈 x : 5 〉 ... ] Indicating z := a-2 
 local [ 〈 z : 4 , a : 6 〉 , ... 〈 x : 2 〉 ... ] parameter x := 2 
 variable [ 〈 z : 12 , a : 6 〉 , ... 〈 x : 2 〉 ... ] passing schema z := 2*a Pass values back to call site ➎ [... 〈 x : 2 〉 ... ] Remove activation record from runtime stack ➏ Parameter Passing 18

  19. CS 381 • Spring 2016 Call-By-Need Synopsis • 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 Parameter Passing 19

Recommend


More recommend