Consider the following user-defined function:

void Quick_Change (int x, int & y)
{
    if (x == 0)
       y = 10;
   else if (y == 0)
               x = 10;
}

Assume the values in variable A and B are both of int type. How do I find what the values are after execution of the statement Quick_Change(A,B), if the values of A and B are

0 and 5 respectively?
5 and 0 respectively?
5 and 5 respectively?
0 and 0 respectively?

Recommended Answers

All 15 Replies

Assuming you mean without using a program, perhaps using pencil and paper to solve each case? The first parameter is passed by value, so it will remain unchanged in the calling function after the function call; the second parameter is passed by reference, so its value may be changed in the calling function following the function call.

either way

what does the "==" mean?

nvm it means equal to

The == operator compares for equality.

yea

Tell me if this is right

0 and 5 respectively? A=0, B=10

5 and 0 respectively? A=10, B=0

5 and 5 respectively? A=5, B=5

0 and 0 respectively? A=10, B=10

Two of them are wrong.

You need to learn by-reference and by-value parameters.

really which two?

You have 1 and 3 right.

I'll give you a quick explanation.

You have two parameter types to your function: by-value and by-reference.

int x is by-value.
int &y is by-reference.

What this basically means is any change made to x within that function is local to that function. It will not change the of the variable A used to call the function.

By-reference means that any change to y within that function will also change the value of the variable B used to call that function.

How about this then

0 and 5 respectively? A=0, B=10

5 and 0 respectively? A=5, B=0

5 and 5 respectively? A=5, B=5

0 and 0 respectively? A=0, B=10

Looks right.

thanks !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.