Loop function

int a Function (int j, int& k)
{
do
{
j++;
k-=2;
} while (j <= k);
return j;
}

What is the output of the following code fragment?

int x = 3, y = 8;
x = aFunction (x,y);
cout << x << " " << y;

---------------------------------------------------
Is the answer 54?
Why don't you try it?
no the answer is
x = 5
y = 4

so on output it looks like: 5 4
cout << x << " " << y;

EDIT:



Last edited on
Yes, I got it. Ans: 5 4
basically because you declare:

x = aFunction(3, 8); <---- but the 8 is passed by what?

try this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int aFunction(int j, int k);

int main()
{
    int x = 3, y = 8;
    x = aFunction (x,y);
    cout << x << " " << y;

    return 0;
}

int aFunction (int j, int k)
{
    do
    {
        j++;
        k-=2;
    } while (j <= k);

    return j;
}

What is the output of the following code fragment?
Last edited on
The correct ans is 5 8?
The correct ans is 5 8?


well yes, but do you understand why ?

x = aFunction(3, 8); <---- but the 8 is passed by what?
Last edited on
Not to sure. I believe in
x = aFunction(3, 8); // y = 8 remains unchanged?
Topic archived. No new replies allowed.