Return DIRECTLY to main?

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? 
Last edited on
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__
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

int a = 0;

void fourth()
{
    if(a==3)
    {
       std::cout << "Fourth called, are we in main()? \n";
    }
    a++;
    /*way more code*/
}
void third()
{
    if(a==2)
    {
        a++;
        std::cout << "In third, going to fourth \n";
        fourth();
    }
}
void second()
{
    if(a==1)
    {
        a++;
        std::cout << "In second, going to third \n";
        third();
    }
}

int main()
{
    a++;
    second();
    std::cout << "We are in " << __PRETTY_FUNCTION__ << "\n";
   //https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Function-Names.html
}

that said your program does look a bit strange to me, not exactly sure what you're trying to achieve
Last edited on
It's not how functions are supposed to be used.

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.
Last edited on
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!

Thanks a lot guys for the responds!
Last edited on
Topic archived. No new replies allowed.