Consider the following program written in C-like syntax(but not exactly C langua
ID: 3544621 • Letter: C
Question
Consider the following program written in C-like syntax(but not exactly C language):Consider the follwing C-like program: (syntax ofc is not exactly like C)
void swap (int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 1, list[5] = {1, 3, 5, 7, 9}; /* list[0] = 1, list[1] = 3, .... */
swap(value, list[value]); /* here */
}
For each of teh follwing parameter passing methods, what are all of the values of the variables value and list after the call to "swap" (at the point marked 'here')? Explain you answer please....
example answer: value = 1, list = {1, 3, 5, 7, 9}
(a) Pass-by-value
(b) Pass-by-reference
(c) Pass-by-value-result (Assume address binding at entry)
(d) Pass-by-name
Consider the following program written in C-like syntax(but not exactly C language):
Consider the follwing C-like program: (syntax ofc is not exactly like C)
void swap (int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 1, list[5] = {1, 3, 5, 7, 9}; /* list[0] = 1, list[1] = 3, .... */
swap(value, list[value]); /* here */
}
For each of teh follwing parameter passing methods, what are all of the values of the variables value and list after the call to "swap" (at the point marked 'here')? Explain you answer please....
example answer: value = 1, list = {1, 3, 5, 7, 9}
(a) Pass-by-value
(b) Pass-by-reference
(c) Pass-by-value-result (Assume address binding at entry)
(d) Pass-by-name
Explanation / Answer
(a) Only values are passed and nothing else.
So, in swap function, a = 1,b = 3.So nothing changes.After the return of swap function, value = 1 and list = {1,3,5,7,9).
(b) Values are passed w.r.t memory Location.So every time the arguments are passed, the changes takes place at memory location too.So after the return of swap function,
value = 3 and list = {1,1,5,7,9).
(c) value = 3 and list = {1,1,5,7,9).
(d) variable are literally substituted my names,so the swap function looks like this.
temp = value;
value = list[value];
list[value] = temp;
So after swap fn, value = 3 and list = {1,3,5,1,9}