Starting from:

$29

Programming Languages Concepts Homework 3

Programming Languages Concepts

Homework 3
Submission: Please submit your homework via Canvas. It’s okay if you
submit a scanned version of your on-paper answers, but please make sure
your scanned version is legible.
1. (6 points) Consider the following function written in C:
int x=1, y=2, z=3;
void foo(int a, int b) {
a = a + b;
x = x + a;
}
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,y) where all parameters are passed by value-result
(f) foo(x,x) where all parameters are passed by reference
2. (2 points) Consider the following C++ program, where &i means i is
passed by reference:
int bar (int &i) {
i = i + 2;
return 2 * i;
}
void foo1 () {
int x = 2, y = 7, sum;
1
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 : I n t e g e r ;
3 procedure Sub1 (C: I n t e g e r ) i s
4 D : 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 B, 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
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
2
local variables, parameters, the dynamic link, the static link, and the
return address.
3

More products