Hello there, i have a code with many many functions, at an given moment, i need it to DIRECTLY go back to the main function, to do nothing more between command to go back and main function, how i can do that? Thanks in advance!
If i try to write the fourth founction (that needs to go back to main) after the main in my code, it will give an error saing that i not declared the fourth function, if i try to write it before main function it says thet i not declared the main function, how to fix that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//Example of what is it like
int a;
void fourth(){if(a==3){ main() }
a++; /*way more code*/ }
void third(){if(a==2){a++; fourth();}
void second(){if(a==1){a++; third();}
int main()
{
a++;
second();
}
//HOW?
main() is the calling function for any c++ program and you don't have to do anything to return control to main() after fourth() returns. In gcc, this can be checked with the magic identifier: __PRETTY_FUNCTION__
A function should carry out some specific task or compute some value. When it has finished, the execution continues from the location where the function was called.
A common mistake that beginners often do is to use functions to keep track of state. That can work for very simple cases but in general you should use data to keep track of state. Functions should instead be used to modify the state.
i need it to DIRECTLY go back to the main function
By 'directly' do you mean jumping all the stack of the calling functions?
I'm not aware of any method to do that, but an unmanaged exception will rise up to main() and then, if not caught even there, cause the program to exit. But... is throwing an exception just to get through the stack an acceptable practice?
+gunnerfunner Thanks a lot for that indentifier, it will be usefull for next projects :) but thats its not what i need now, thanks anyways. +Peter87 Yes, i do that, i didnt know thats its bad, i will rewrite my code without it. Thanks for the tip! +Enoizat Yes, i will not need anymore because +Peter87 just told me the mistake that i made, thanks anyways!