Parameter Passing Presentation

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Parameter Passing

Parameter Passing
Logically parameter-passing has following possible semantics:
IN: pass info from caller to callee
OUT: callee writes a value in the caller
IN/OUT: caller tells callee value of var, which may be updated by
callee

However, different mechanisms of implementing IN/OUT can
have subtle semantic differences.

Some languages support many param passing modes (C++: by
value, by reference, by const-reference; array-mode), some few
(Java: by value).

Techniques Used for
Parameter Passing
call by value
call by result
call by value-result
call by reference
call by name

Call By Value
IN mode:
cant have an effect on caller variables
unless value passed is a pointer (C) or reference (Java)

Used by many languages, incl.: C/C++, Java, Algol60

How it works:
Formal parameter is just like a local name: storage of formals in
activation record of called procedure.

Caller evaluates the actual parameters and sticks their R-
values in the storage for the formals
Example for Call By Value
#include <stdio.h>

void func (int a, int b){
a += b;
printf("In func, a = %d b = %d\n", a, b);
}

int main(void){
int x = 5, y = 7;
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}

OUTPUT:
In func, a = 12 b = 7
In main, x = 5 y = 7


Call By Result
The formal parameter is just like a local variable in the
activation record of the called procedure( it is uninitialized)

After the called method finish execution, the final value of the
formal parameter is assigned to the corresponding actual
parameter

It is also called copy-out

It was introduced in ALGOL 68

Sometimes used in Ada
Example for Call By Result
Example for Call By Result
Call By Reference
IN/OUT mode

General idea: the formal and actual refer to the same memory
location

example languages: C++, Pascal

How it works:
If an actual param is a name, or an expression having an L-value, the
L-value (i.e., address) is passed.
if it doesnt have an L-value (e.g., i+3), then eval the expression in a
new temp loc, and pass that address
in called proc., code generated for a reference to the formal
dereferences the pointer passed

Example for Call By Reference
#include <stdio.h>
void swapnum(int &i, int &j) {
int temp = i;
i = j;
j = temp;
}
int main(void) {
int a = 10;
int b = 20;
swapnum(a, b);
printf("A is %d and B is %d\n", a, b);
return 0;
}


OUTPUT:A is 20 and B is 10
Call by Value-Return/Result
IN/OUT mode

General idea: copy the value in at call, then copy back at
return

example languages: some implementations of Fortran (other
impls. use call by reference)

How it works:
Caller evals actual params. R-values are passed (as in pass by
value). L-values are also computed.
On return from called routine current R-values of formals are
copied back into L-values of actuals.

Example for Call by Value-
Return/Result
begin
integer n;
procedure p(k: integer);
begin
n := n+1;
k := k+4;
print(n);
end;
n := 0;
p(n);
print(n);
end;

OUTPUT: 1 4

Comparison of Call-by-Reference
and Call-by-Value-Return
value-return has higher cost at call/return time, but lower cost
per variable reference (no-deref. necessary)

Could use value-return in situation where called proc uses a
different address space (e.g., remote procedure call (RPC)).

Danger: in some cases get different results from value-return
and reference.
Means mechanism needs to be specified by language designer:
older Ada and Fortrans gave implementers the choice.

Call by Name
IN/OUT mode

General idea: similar to macro-expansion

example languages: Algol-60 (first structured language), some functional languages
(Miranda, Haskell)

How it works:
proc. call done as macro expansion: actual parameters substituted for formals in all places
formals occur in proc. (i.e. variable ref sites)
local names are kept distinct (in case of conflicts)
actuals surrounded by parentheses to preserve precedence of operations
The chunk of code to eval the actual in the calling environment is called a thunk .

Why would we want to do this?
lazy evaluation:
Only eval params when needed. will save time if parameter is not needed at all.
Function call is much less expensive.
also called late binding of parameters

Disadvantages: if some ops have side effects, or with aliasing, sometimes difficult to
anticipate results (more error-prone programs?).
Use of call-by-name in functional
languages
in purely functional languages expressions have no side effects. I.e.,
get same result if you eval it one time or ten times. (Thus, call by
name is for IN only)

lazy eval. means may eval expr. zero times

memoization means well eval expr. at most one time.

memoization idea:
1
st
time you eval param (i.e., at first reference site), save its value.
next time, use the already computed value
(i.e., if 10 references, only compute the value the first time).
E.g., suppose the value being computed is the result of fib(10), you
dont have to do the repeated recursive calls more than once.
Example for Call By Name
Comparison of Call by Value and
Call by Name
CBN is flexible- strictly more programs terminate
E.g., where we might have an infinite loop with cbv, we might
avoid it with cbn by waiting to evaluate

Order of evaluation is really hard to see in CBN
Call-by-name doesn't mix well with side effects (assignments,
print statements, etc.)

Call-by-name is more expensive since
Functions have to be passed around

If you use a parameter twice in a function body, its thunk (the
unevaluated argument) will be called twice
Specification Issues
Are these just implementation techniques, or part of the
language specification?

Depends on the language:
Without side-effects, parameter-passing technique may be
undetectable by the programmer
Even with side effects, some languages specify the parameter
passing technique only partially

REFERENCES:
http://www-scf.usc.edu/~csci410/notes/paramPass.pdf
http://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
http://courses.cs.washington.edu/courses/cse505/99au/imper
ative/parameters.html
http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PA
RAMS.html
https://publib.boulder.ibm.com/infocenter/comphelp/v8v101
/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fre
f%2Fpass_by_value.htm
https://www.cs.umd.edu/class/spring2012/cmsc330/lectures/
17-parameterPassing.pdf
http://www.cs.ucf.edu/courses/cop4020/spr2007/lecture4.pp
t

You might also like