Homework 8: Functions and arguments
- Print version, with cover page (pdf)
- Due Date: Thursday, October 26
1 Pass by what?
Here is a small SPL program with a single function that gets called twice.
new a := 20;
new b := 10
new foo := lambda x {
x := x + x;
ret := x * b;
}
write foo@a;
write a;
write foo@b;
write b;
Clearly four numbers will get printed by this piece of code. Tell me what those four numbers will be under:
- Pass by value
- Pass by reference
- Pass by value/result
2 Value/result example
In Java function parameters are passed by sharing by default. In class, we saw a different kind of parameter passing mode called pass by value/result. Write a small Java program demonstrating that function parameters in Java really are passed by sharing and not by value/result. That is, your program should do something different in each of these parameter passing modes.
As always, I want you to come up with your own examples! You can work together on homeworks, but the examples you turn in should be unique.
Clearly specify what the result of your function would be under pass by sharing and under pass by value/result.
3 Barbarian Sausage Gang
Consider the following Scheme program:
(define (f x y)
(display x)
(display 'a)
(display y)
x)
(define (g x y)
(cond ((eqv? x 'g)
(display x))
((eqv? x y)
(display 'e))))
(g (f ?? ??) (f ?? ??))
Fill in the
??
blanks in the function call so that the program printsbarbarian
under call-by-name evaluation rules.Fill in the
??
blanks in the function call so that the program printssausage
under applicative-order evaluation rules.Fill in the
??
blanks in the function call so that the program printsgang
under normal-order evaluation rules.