


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



The function call is one of the most fundamental elements of programming. There are many dif- ferent strategies for getting the parameters from the function’s caller into the function when the function is called. These strategies are called pa- rameter passing styles. The choice of parameter passing style encompasses several other choices, such as when parameters are evaluated, where the parameters are kept in memory, and whether changes made to parameters inside a function can be seen outside the function. We will cover five parameter passing styles: Call By Value, Call By Reference, Call By Value- Result, Call By Name, and Call By Need. Your objective is to be able to explain these styles. You should be able, given a block of sam- ple code, to show the result for each of these styles. You should also be able to explain how each of these would be implemented.
The most familiar parameter passing style for most of you will be call by value (CBV). Lan- guages such as Java, C++, OCaml, Scheme, For- tran, and Python all use this style. In this style, parameters are evaluated before the function call takes place, and the function receives a copy of the result. The justification for this is simple: if we thought it worth the trouble to pass a computation into a function, it is likely because we thought the function would
1 int x = 10; 2 3 int foo(int a, int b) { 4 a := a ∗ 2; 5 b := b + x; 6 return a + b; 7 } 8 9 print(foo(x,10+20));
Figure 1: Code for foo
need the result of that computation. Further, passing a value into a function is a lot less effort than passing in an unevaluated expression. Be- cause parameters are always evaluated, we often refer to this style as eager evaluation. To implement this, the system will allocate new memory for the parameters of the function just before the function call. This memory will not (normally) be available outside of the func- tion. For example, consider the C-like pseudo-code in figure 1. When foo is called, there will be three vari- ables in memory: a and b on foo’s stack frame, and x in the main memory. Just after foo starts, the memory will look like figure 2.
Var Value x 10 a 10 b 30
Figure 2: Start of CBV foo call
Just before foo returns, the memory will look like figure 3. The values of a and b will have changed. They will be destroyed once foo re- turns.
Var Value x 10 a 20 b 40
Figure 3: End of CBV foo call
The return value, a + b, is 60. The two important things to note are that x does not have a change of value, and that the pa- rameters are evaluated before the function call. The advantage of CBV is speed. Pushing in- teger results onto a stack is fast (and some ar- chitectures don’t even need to do that—they can put the parameters into a register instead). The disadvantage is that a parameter is always evaluated, even if it is not needed. This can be a source of inefficiency, and even instability. Consider figure 4 as an example. What do you think will happen when foo is called? Mathe- matically, the return value should be 5, because the b parameter is unused. But, because all argu- ments are evaluated before the function is called, we get an infinite loop instead.
1 int pi1(int a, int b) { 2 return a; 3 } 4 5 int foo() { 6 return pi1(5,foo()); 7 }
Figure 4: Unstable CBV call
Call By Reference is also a very common param- eter passing style, perhaps as common as Call By Value. In this style, we expect to pass a variable to a function, not an expression. This is because
no memory is allocated for the new variable. In- stead, the variable bound to the parameter will have the same location as the parameter itself. The function doesn’t get a copy, but the vari- able location itself. As a result, there are two variables, potentially with different names, that share the same memory location. An update to one variable immediately is visible in the other. The phenomenon of having one memory ad- dress with two names is called aliasing. If this is what you want, it’s a very powerful technique. If it is not what you expected, it is a great source of hard to find errors. Going back to our running example, suppose that the first argument to foo is passed by ref- erence. The memory diagram would look like figure 5 at the start of the call, and figure 6 at the end of the call.
Var Value x,a 10 b 30
Figure 5: Start of CBR foo call
Notice that the assignment in line 5 behaves differently now. Line 4 had set the value of a and x, since they are really the same thing.
Var Value x,a 20 b 50
Figure 6: End of CBR foo call
The return value, therefore, is 70.
3 Call By Value-Result
There is an unusual style related to Call By Ref- erence, called Call By Result, or sometimes Call By Value-Result (CBVR). It simulates Call By Reference, in that the system allocates memory for the function’s variables, but then at the end of the function call copies the results back to the caller’s variables.
returns for each of the eager parameter passing styles?
1 main () = 2 let a = 5 in 3 let b = 7 in 4 let foo x y z = 5 x := z + z + y; 6 y := 9 + a; 7 x + y 8 in 9 foo a b (a+b)
Consider the following code. How many times is + executed for Call By Value, Call By Name, and Call By Need?
1 foo a b c = 2 a ∗ a ∗ c 3 4 main () = foo (10+10) (20+20) (30+30)
Solutions are in the question database, ques- tions x and y.