$30
CMPSC 461: Programming Languages Concepts
Homework 3
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
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.
1 procedure Main i s
2 A, B, C : I n t e g e r ;
3 procedure Sub1 (C: I n t e g e r ) i s
4 D, E : I n t e g e r ;
5 begin −− o f Sub1
6 . . .
7 end ; −− o f Sub1
8 procedure Sub2 (E: I n t e g e r ) i s
9 procedure Sub3 (F: I n t e g e r ) i s
10 C, D: I n t e g e r ;
11 begin −− o f Sub3
12 Sub1 ( 1 0 0 ) ;
13 end ; −− o f Sub3
14 begin −− o f Sub2
15 Sub3 (E ) ;
16 end ; −− o f Sub2
17 begin −− o f Main
18 Sub2 ( 1 0 ) ;
19 end ; −− o f 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