Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1, C++ A. Describe the difference between how a primitive variable is passed to

ID: 3809450 • Letter: 1

Question

1, C++

A. Describe the difference between how a primitive variable is passed to a function versus how a reference variable is passed to a function.

B. Declare an enumerated type of kitchen appliances, declare a variable of that type, and assign a value to it.

C. What is the scope of variable V bellow?

int changeVal(int w)
{
static int v = 12;
v = v * w;
return v;
}
int main()
{

changeVal(2);

changeVal(3);

changeVal(4);

changeVal(5);

}

output: 24
   72
   288
   1440
---------------------------------------------------
What is the scope of variable v?

Explanation / Answer

Ans1:

Consider a swapping function to demonstrate pass by value vs. pass by reference.
        main() {

First, we show the memory picture for swapThemByVal. The activation record holds the memory for the two parameters, num1 and num2, and the local variable, temp. A copy of the values from main, in the contents of i and j, are copied. All the manipulation is done in the activation record.

The contents of memory of i and j don't change. The contents of memory in the function's activation record changes, but when the function terminates, the memory is released and the changes are lost.

Contrast this with passing by reference. The addresses of i and j are passed (noted by the arrows) by reference. The compiler knows they are references so when the parameters are referred to in the function, the compiler dereferences num1 and num2 automatically so i and j of main's memory are changed.

After the assignments:

NOTE:

Ans B: