Homework 9: Functions and arguments
- Print version, with cover page (pdf)
- Due Date: Monday, November 8
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 Overloading and polymorphism
Answer the following questions to demonstrate your knowledge of these concepts.
Why doesn’t Python support function overloading the way C++ and Java do?
What would need to be added to the SPL language in order to make function overloading possible?
What would need to be added to the SPL language in order to make subtype polymorphism possible?
The following code works in C++ and prints “2”:
bool x = true; cout << x + x << endl;
But the C++ standard says that the
+
operator only works for numeric types, not forbool
s. What’s going on here?
4 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.