Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec. 13.2 Procedure declaration procedure p( a, b : integer, f :

Similar presentations


Presentation on theme: "Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec. 13.2 Procedure declaration procedure p( a, b : integer, f :"— Presentation transcript:

1 Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec. 13.2 Procedure declaration procedure p( a, b : integer, f : float ) Name Formal parameter list Formal parameter list (formal arguments) Procedure Call procedure p( x, x+y, 67.0 ) Name Actual parameter list Actual parameter list (formal arguments) Code invoking a subroutine (either a procedure or function ) is the caller Invoking routine is the cal ler

2 Lecture 16 Parameter – Passing Mechanisms Rules governing how value passed from actual to formal parameters upon call and back on return Call by value (C, Pascal) Call by reference (Pascal, C++) Call by value result or copy restore (Fortran) Call by name (Algol 60) Program’s result can totally change with a different mechanism The language specifies a mechanism Distinction between values and locations An r–value is the value of an expression or variable »Evaluation done on RHS of assignment An I–value is the location of a variable »Evaluation done on LHS of assignment

3 Lecture 16 Call by value Evaluate each actual argument and pass a copy of value to routine Pass r-value, not I-value Assignment to formals in routine do not affect actual Program() procedure swap (x, y : integer) var i, j : integer var tmp : integer begin i := 6; tmp := x; j := 10; x := y; swap ( i, j ) y := tmp; write ( i, j ); end; End; Used by C for all parameters. Used in Pascal for non-var parameters

4 Lecture 16 Call by Reference Evaluate each actual argument and pass its I-value If actual is vanable or location in data structure ( e.g. A[i] or x  foo), pass address of the location. If actual is expression, evaluate expression, store in memory and pass address –Or, language may forbid passing expressions by reference actual parameters Modifications to call by reference formal arguments modify actual parameters. Program() procedure swap (var x, y : integer) var i, j : integer var tmp : integer begin i := 6; tmp := x; j := 10; x := y; swap ( i, j ) y := tmp; write ( i, j ); end; end;

5 Lecture 16 Call by Reference, cont’d Used for Pascal var parameters Simulated in C by passing address with & operator Famous example from Fortran II Procedure swap (x : integer) x := x + 1; mistake (100100100); write (100100100); if large numbers stored uniquely in memory to save space, then the program can change constants –Compiler ( and maybe language design) error

6 Lecture 16 Call by Value-Result Caller passes value of actual argument to routine Also calculates and saves I-value of actuals When callee returns, caller copies final value of an argument back into the location of actuals Changes to formals affect actuals f(x) At Call At Return formal = x; *xaddr = formal; xaddr = &x; Similar to call by reference, but can produce different results since caller’s values do not change until return Procedure p (a, b : integer{ a := a + 1; b := 44;} at caller I := 3; p ( i, array (i) ); By value result modifies a[4] By reference modifies a[3]

7 Lecture 16 Call by Name Conceptually, the actual parameters are substituted into the body of the routine before invocation Variables are renamed to keep them distinct Used in definition of Algol 60 Interestingly, cannot write a swap routine with this mechanism Swap ( a, b : integer ) Swap ( i, a[i] )  tmp := a; tmp := i; a := b; i := a[i]; b := tmp; a[i] := tmp; i modified

8 Lecture 16 Call by Name, cont’d Call by name implemented with thunks A thunk is a piece of code that evaluate to I-value of actual argument –Code is run in environment of caller Thunk passed as argument –Callee executes code each time arguments is used

9 Lecture 16 Aliasing Two expressions that denote the same memory location are aliases e.g. a[i+1] and a[j] when i = j -1 Modification to one place may change apparently distinct values Subroutine calls are a rich source of aliases Can pass the same argument twice: »p ( x, y, z ) procedure p ( a, b, c ) Can pass a global variable as an actual »p ( g, x,y ) global variable used in p


Download ppt "Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec. 13.2 Procedure declaration procedure p( a, b : integer, f :"

Similar presentations


Ads by Google