Homework 9

This is the archived website of SI 413 from the Fall 2013 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

1 Pass by what?

Here is a small SPL program with a single function that gets called twice. (Not that it matters, but you may assume lexical scoping here.)

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:

  1. Pass by value
  2. Pass by reference
  3. 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.

  1. Why doesn't Python support function overloading the way C++ and Java do?

  2. What would need to be added to the SPL language in order to make function overloading possible?

  3. What would need to be added to the SPL language in order to make subtype polymorphism possible?

  4. 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 for bools. What's going on here?

4 Hello, world!

Write a C++ program that prints (exactly) the string "Hello, world!" (followed by a newline), and demonstrates assignments using as many different kinds of l-types that you can in C++.

For full credit, your assignments should be somehow meaningful or useful. So for example, the program

int a = 3;
a++ = 29;
cout << "Hello, world!" << endl;

would not receive full credit, because the assignments with a had nothing to do with the output. Something like

string s = "Hello, vorld!";
s[7] = 'w';
cout << s << endl;

would be more like it. But of course you will need to have more assignments than this to demonstrate all the different kinds of l-values!

Print out your code and turn it in. Within your code printout, circle all the l-values that you see.