Functions

Here's my code and I'm not able to explain the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int exp(int&);
int main()
{
 int a=10;
 for(int i=0; i<=a; i+=exp(i))
 {
	puts("STRING");
 };
 return 0;
}
int exp(int &a)
{
 int i;
 i=a;
 i++;
 return i;
 i+=2;
}


It's printing STRING 4 times, can anyone explain why?
PS I compiled it in Turbo C++ if it matters.
Thanks!
Could you please, to your understanding of the function exp in the way you have written it, explain what it does. Explain what each line of the function does.
Well, I read it somewhere that as soon as a function encounters a return statement it stops execution, so I think exp should never execute the i+=2, in fact it should never even come across it. And, moreover a(in exp) is the alias of i (in the main) so any change in a(in exp) should be reflected in i ( in the main) but a is never being changed in exp, it is just being assigned to i ( in exp) and i is getting incremented each time. So I think
for (int i=0; i<=a; i+=exp(i))
is equivalent to
for (int i=0; i<=a; i++)

Please tell me where am I going wrong? :/
Last edited on
closed account (zb0S216C)
Your exp() function is pointless - what's the goal of exp() here?

Dhanshree Arora wrote:
1
2
return i;
 i+=2; // Ignored. 

Once return is reached, any subsequent statements are ignored. You already know this, so why keep it there?

Dhanshree Arora wrote:
"should be reflected in i ( in the main) but a is never being changed in exp"

This is because i (in exp()) is a copy of i in main(). Since a is an alias, working with it is the exact same as working with the referent directly. So in essence, you're copying i in main().

Wazzak

Last edited on
^ Yes, I know that. So technically, STRING should be printed 11 times right? But it's being printed only 4 times. Can you please explain that?
Last edited on
Because of the way you increment i in your for loop.
the value of i used for that for loop will be 0 1 3 7 which obviously is 4 times, therefore, your code printed STRING 4 times.

trace it by substitute actual value to i starting with 0.
you are passing a reference to exp() , so the changes within that function will change the value of i in main.

am I making any sense?
Last edited on
This is your incrementation of the interator i i+=exp(i)
ThangDo is right.


ThangDo

trace it by substitute actual value to i starting with 0.
you are passing a reference to exp() , so the changes within that function will change the value of i in main.




I thought of that too, but am getting confused while I try to trace the entire process 'cause of the return statement. I am perceiving it this way, for i=0 in main, STRING is printed once. Then exp is called where i if referenced to a, and a assigns 0 to i in exp which increments once and returns the value.
1. Shouldn't the function STOP execution 'cause of this return?
2. If it does not and carries out i+=2, how does this affect the i in main? i (in exp) is not referenced to i(in main), and a(in exp) undergoes no change to change i (in main). Let's suppose it does, then how will you trace it? Can you please elaborate?
Values of i in main are these,

1)0
2)1
3)3
4)7
5)15 //for loop stops

So 4 time printed
1
2
return i;
 i+=2; // Ignored. 


i += 2; is ignored because of the return above it. I think you get this part.
It make no effect to i in main.

if you changed it
1
2
i += 2;         //Not ignored anymore
return i;


then the value of i in main() will be referenced to increased by 2 as well (by saying this, it means in addition to whatever changes made to i previously)

one thing is that you should give variables different names to avoid being confused of which is being used/talked about.

Topic archived. No new replies allowed.