Flow of c++ code..

Just a simple question...

code:
void haha(int k)
{

bebe(k); //another function call



for(int i = 1; i<10; i++)
{
if(condition)
{
do1;
do2;
do3;

haha(k + 1);

do4;
do5;
}
}


}

Will the programme ever go to 'do4' and 'do5'?
I dont think it will...
but the output is completely different if i remove them..

why is this so??
closed account (S6k9GNh0)
Why do you not think they will reach those sections?
Because it is inside an IF-statement?
and i thought void function is a function that does not return anything?
Your example has the part outside the if-statement. so It will execute.
On the other hand, my example an if-statement, and inside it it has a function caller (of the same function).
So i thought do4 and do5 is never reached..
wrong?
and i thought void function is a function that does not return anything?

A function which has a return type of type void does not return a value. That does not mean it does not return.

What would you expect the output of the following program to be?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

void somefunc()
{
}

int main()
{
    somefunc() ;
    std::cout << "This line is reached.\n" ;
}


Your example has the part outside the if-statement. so It will execute

Completely irrelevant. If the function didn't return, whether the statements were inside an if statement or not would make no difference. They wouldn't be executed in either instance.


On the other hand, my example an if-statement, and inside it it has a function caller (of the same function).
So i thought do4 and do5 is never reached..
wrong?

Obviously it is wrong as indicated by your own observation.
Last edited on
Wrong, as in my code is wrong?
Back to my code, so do4 and do5 are executed when the function returns right?
When does that happen? (according to my code)
For example, when the condition in if-statement doesn't hold.

in other word, when does the function return?
(code consists of if-statement inside for loop, and a funtion caller of the same function inside it)
Wrong, as in my code is wrong?

Wrong as in "So i thought do4 and do5 is never reached.." is wrong.


Back to my code, so do4 and do5 are executed when the function returns right?

Correct.


in other word, when does the function return?

When there have been 9 iterations of the for loop.
Then what is the difference between the original one and this one?


void haha(int k)
{

bebe(k); //another function call



for(int i = 1; i<10; i++)
{
if(condition)
{
do1;
do2;
do3;

haha(k + 1);


}
do4;
do5;
}


}

I moved do4 and do5 out of the if-statement?
Dont they do the same thing?
Last edited on
Then what is the difference between the original one and this one?
I moved do4 and do5 out of the if-statement?


If you move do4 and do5 outside the body of the if statement, then their execution no longer depends on the expression that controls whether the if statement is entered or not. do4 and do5 will be executed for every iteration of the loop regardless of what condition evaluates to.

There is a tutorial on control structures on the site here if you need a refresher.
http://www.cplusplus.com/doc/tutorial/control/
Oh yea.. i got that point..
but i still dont understand why after the for loop execution?
but i still dont understand why after the for loop execution?

If you mean you don't understand why the function returns after the for loop I really don't see the difficulty. That's where the function ends. What happens when a function ends? It returns.
But the function is called before it ends right?
inside the loop so i was thinking right after the function is called, the for loop resets
But the function is called before it ends right?

It may or may not have been. What's important is that any invocations of haha made by a given invocation, X, of haha have also returned prior to X reaching the end of the function and returning.

If you run the example given in the other thread, this is illustrated by the output. The "level" corresponds to a particular invocation of the function. Smaller levels are nested invocations.
Last edited on
So, regardless of the function being called, do4 and do5 are executed after the for loop of ends?
but if the function was called, the fucntion re-starts from line 1 right? (im referring to haha(k+1))
You made me more confused :(:(
sorry i am bad at this.. but willing to get better
So, regardless of the function being called, do4 and do5 are executed after the for loop of ends?

No. The function ends/returns after the for loop is executed. Why you want to conflate that with the execution of do4 and do5 is a mystery to me. do4 and do5 are executed for each iteration of the loop in which condition evaluates to true, prior to the for loop ending. I refer you again to the control structure tutorial linked before.

but if the function was called, the fucntion re-starts from line 1 right?

The function does not "re-start". A new invocation of the function begins with new local variables and a new argument that do not affect the ones belonging to any invocations that are still in progress. This can be seen, again, by studying the output of the example given previously in the duplicate thread.
Oh, so the moment the function is called, the for loop still continues,
but a new function that does the same thing with parameter k+1 forms.
is that what it does?
Topic archived. No new replies allowed.