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.
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)
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.
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.
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.
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))
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?