CS 381 • Spring 2016 Parameter Passing
- 6. Parameter Passing
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
CS 381 • Spring 2016 Parameter Passing
CS 381 • Spring 2016 Parameter Passing
What is the value of z? ... it depends on the parameter passing mechanism.
x := 3; z := 1; f(2*x,z); void f(int x, int y) { y := x+1 };
(Formal) Parameter Actual Parameter, or: Argument
2
CS 381 • Spring 2016 Parameter Passing
3
… parName * … … fName(parName, …)
Interface
Function/procedure/method name ➊ Parameter names and types ➋ Access to arguments ➌
fName(3,x+1,y,…)
Implementation Call
Defines: Pass arguments to parameters ➊ Pass control and execute code ➋ Return values as results ➌
CS 381 • Spring 2016 Parameter Passing
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〉 ...]
CS 381 • Spring 2016 Parameter Passing
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〉 ...]
CS 381 • Spring 2016 Parameter Passing
6
p := … … := p fName(p,…) p
CS 381 • Spring 2016 Parameter Passing
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
CS 381 • Spring 2016 Parameter Passing
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
CS 381 • Spring 2016 Parameter Passing
p := … … := p
9
fName(x,…) fName(ref p,…) x
CS 381 • Spring 2016 Parameter Passing
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
CS 381 • Spring 2016 Parameter Passing
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
CS 381 • Spring 2016 Parameter Passing
p := … … := p
12
fName(x,…) fName(valres p,…) x
copy in copy out
p
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 }
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
CS 381 • Spring 2016 Parameter Passing
\
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
CS 381 • Spring 2016 Parameter Passing
\
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]
CS 381 • Spring 2016 Parameter Passing
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
CS 381 • Spring 2016 Parameter Passing
… := … p
17
fName(e,…) fName(name p,…) … := … (e)
CS 381 • Spring 2016 Parameter Passing
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〉 ...]
CS 381 • Spring 2016 Parameter Passing
19
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
copy value to actual param
20
CS 381 • Spring 2016 Parameter Passing
21
CS 381 • Spring 2016 Parameter Passing
22
Haskell