CMPSC 461: Programming Languages Concepts
Prof. G. Tan Spring 2020
Homework 3: Due on Feb 29th at 6pm in Gradescope.
Submission: Please submit your homework via Gradescope. During submission, you need to match pages and homework questions.
You can watch a video about how to do that via Gradescope below: https://www.youtube.com/watch?time_continue=1&v=KMPoby5g_nE&feature= emb_logo
If you submit a scanned version of your on-paper answers, please make sure your scanned version is legible.
1. (6 points) Consider the following function written in C:
int x=3, y=2, z=1;
void foo(int a, int b) {
x = x + b;
a = a – b; }
In each of the cases below, write down the values of x, y and z after the following calls to foo(). If necessary, assume that output arguments are copied back to parameters in the left-to-right order.
(a) foo(y,z) where all parameters are passed by value
(b) foo(y,z) where all parameters are passed by reference
(c) foo(y,z) where all parameters are passed by value-result (d) foo(x,y) where all parameters are passed by reference (e) foo(x,x) where all parameters are passed by reference
(f) foo(x,x) where all parameters are passed by value-result
2. (2 points) Consider the following C++ program, where &i means i is passed by reference:
int bar (int &i) {
i = i – 2;
1
1 2 3 4 5 6 7 8 9
10
11
12
13
14
15
16
17
18
19
procedure Main i s
A, B, C : Integer ;
procedure Sub1 (C: Integer ) is
D, E : Integer ;
begin −− of Sub1 …
end; −− of Sub1
procedure Sub2 (E: Integer ) is
procedure Sub3 (F: Integer ) is C, D: Integer ;
return 2 * i; }
void foo1 () {
int x = 3, y = 6, sum;
sum = x;
sum = sum + bar(x);
}
void foo2 () {
int x = 2, y = 7, sum;
sum = bar(x);
sum = sum + x;
}
(a) What is the value of sum at the end of the function foo1? Briefly explain why.
(b) What is the value of sum at the end of the function foo2? Briefly explain why.
3. (4 points) Consider the Ada program given below.
begin −− of Sub3 Sub1(100);
end; −− of Sub3 begin −− of Sub2
Sub3(E);
end; −− of Sub2
begin −− of Main Sub2(10);
end; −− of Main
2
Ada is a statically scoped language. In the above program, the Main function invokes Sub2; Sub2 invokes Sub3; and Sub3 invokes Sub1. Draw the stack of activation records when the program’s execution reaches before line 12 and line 6. For each activation record, include local variables, parameters, the dynamic link, the static link, and the return address.
3