Post on 03-Jun-2018
8/12/2019 Parameter Passing Presentation
1/19
Parameter Passing
8/12/2019 Parameter Passing Presentation
2/19
Parameter PassingLogically 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 canhave subtle semantic differences.
Some languages support many param passing modes (C++: byvalue, by reference, by const-reference; array-mode), some few(Java: by value).
8/12/2019 Parameter Passing Presentation
3/19
Techniques Used forParameter Passing
call by value call by result call by value-result call by reference call by name
8/12/2019 Parameter Passing Presentation
4/19
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
8/12/2019 Parameter Passing Presentation
5/19
Example for Call By Value#include
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 = 7In main, x = 5 y = 7
8/12/2019 Parameter Passing Presentation
6/19
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 theformal parameter is assigned to the corresponding actualparameter
It is also called copy-out
It was introduced in ALGOL 68
Sometimes used in Ada
8/12/2019 Parameter Passing Presentation
7/19
Example for Call By Result
8/12/2019 Parameter Passing Presentation
8/19
8/12/2019 Parameter Passing Presentation
9/19
Call By Reference IN/OUT mode
General idea: the formal and actual refer to the same memorylocation
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
8/12/2019 Parameter Passing Presentation
10/19
Example for Call By Reference#include 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
8/12/2019 Parameter Passing Presentation
11/19
Call by Value-Return/Result IN/OUT mode
General idea: copy the value in at call, then copy back atreturn
example languages: some implementations of Fortran (otherimpls. 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.
8/12/2019 Parameter Passing Presentation
12/19
Example for Call by Value-Return/Resultbegin
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
8/12/2019 Parameter Passing Presentation
13/19
Comparison of Call-by-Referenceand Call-by-Value-Return
value-return has higher cost at call/return time, but lower costper variable reference (no-deref. necessary)
Could use value-return in situation where called proc uses adifferent address space (e.g., remote procedure call (RPC)).
Danger: in some cases get different results from value-returnand reference.
Means mechanism needs to be specified by language designer:older Ada and Fortrans gave implementers the choice.
8/12/2019 Parameter Passing Presentation
14/19
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 toanticipate results (more error-prone programs?).
8/12/2019 Parameter Passing Presentation
15/19
Use of call-by-name in functionallanguages
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 byname is for IN only)
lazy eval. means may eval expr. zero times
memoization means well eval expr. at most one time.
memoization idea: 1st 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 .
8/12/2019 Parameter Passing Presentation
16/19
Example for Call By Name
8/12/2019 Parameter Passing Presentation
17/19
Comparison of Call by Value andCall 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 (theunevaluated argument) will be called twice
8/12/2019 Parameter Passing Presentation
18/19
Specification Issues Are these just implementation techniques, or part of thelanguage 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
8/12/2019 Parameter Passing Presentation
19/19
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/PARAMS.html https://publib.boulder.ibm.com/infocenter/comphelp/v8v101
/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%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.ppt
http://www-scf.usc.edu/~csci410/notes/paramPass.pdfhttp://msdn.microsoft.com/en-us/library/zthk2dkh.aspxhttp://courses.cs.washington.edu/courses/cse505/99au/imperative/parameters.htmlhttp://courses.cs.washington.edu/courses/cse505/99au/imperative/parameters.htmlhttp://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PARAMS.htmlhttp://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PARAMS.htmlhttps://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/pass_by_value.htmhttps://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/pass_by_value.htmhttps://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/pass_by_value.htmhttps://www.cs.umd.edu/class/spring2012/cmsc330/lectures/17-parameterPassing.pdfhttps://www.cs.umd.edu/class/spring2012/cmsc330/lectures/17-parameterPassing.pdfhttp://www.cs.ucf.edu/courses/cop4020/spr2007/lecture4.ppthttp://www.cs.ucf.edu/courses/cop4020/spr2007/lecture4.ppthttp://www.cs.ucf.edu/courses/cop4020/spr2007/lecture4.ppthttp://www.cs.ucf.edu/courses/cop4020/spr2007/lecture4.ppthttps://www.cs.umd.edu/class/spring2012/cmsc330/lectures/17-parameterPassing.pdfhttps://www.cs.umd.edu/class/spring2012/cmsc330/lectures/17-parameterPassing.pdfhttps://www.cs.umd.edu/class/spring2012/cmsc330/lectures/17-parameterPassing.pdfhttps://www.cs.umd.edu/class/spring2012/cmsc330/lectures/17-parameterPassing.pdfhttps://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/pass_by_value.htmhttps://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/pass_by_value.htmhttps://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/pass_by_value.htmhttp://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PARAMS.htmlhttp://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PARAMS.htmlhttp://courses.cs.washington.edu/courses/cse505/99au/imperative/parameters.htmlhttp://courses.cs.washington.edu/courses/cse505/99au/imperative/parameters.htmlhttp://msdn.microsoft.com/en-us/library/zthk2dkh.aspxhttp://msdn.microsoft.com/en-us/library/zthk2dkh.aspxhttp://msdn.microsoft.com/en-us/library/zthk2dkh.aspxhttp://www-scf.usc.edu/~csci410/notes/paramPass.pdfhttp://www-scf.usc.edu/~csci410/notes/paramPass.pdfhttp://www-scf.usc.edu/~csci410/notes/paramPass.pdf