Return to int main from an int.

Hi, just wondering how would I return to the "int main" from another int?

For example

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
40
41
42
43
44
45
46
47
48
49
50
51
52

int CallInfo()
{
   string sClientName;
   int sClientChoice;
   cout << "Hello, what's your name?" << endl;
   cin >> sClientName;
   cout << "Hello " << sClientName << endl;
   cout << "Enter either 1 to exit program or 2 to go back to main menu" << endl;
   cin >> sClientChoice;
   
   if (sClientChoice == 1)
   {
      system("exit");



    }
else if (sClientChoice == 2)
{
     //RETURN TO INT MAIN SOMEHOW??



}

}

int main()
{
    int sClientAge;
    cout << "Hello" << endl;
    cout << "How old are you?" << endl;
    cin >> sClientAge;
    if (sClientAge >= 18)
    {
      CallInfo();


    }
    else
    {

       cout << "Sorry, you're not old enough" << endl;
       Sleep(2000);
       system("exit");

    }

}





Please take in mind that this is just a quick example to get my point across hence why they're no includes xD
With the 'return' keyword
1
2
3
4
else if (sClientChoice == 2)
{
  return 0;
}


You are not using the return value of the function though. Maybe it's better if you declare it as returning void. A void function can use the return keyword with no arguments.
You should think of a function as something that does some specific task. When the task is finished the function returns. The function should not have to care about where it returns to. It just returns to the code that called the function.

A function will automatically return when the function ends. If you want to end the function earlier and/or you want to return a value from the function you can use a return statement. To return from a void function you just do return;. If the function has a return type, other than void, you add the value that you want the function to return after, so if you want CallInfo() to return the value 5 you write return 5;.
Last edited on
If I put that return 0 in it will just exit the problem either way :(

So to be clear, they're no possible way to return int main? Unless I make it a void which in that case how would the program know what to start with?
Last edited on
there's no possible way to return int main?

False.

If you use return 0; in function CallInfo(), the program definitely does return to main.

It will return to main() at line 38.
When it reaches the closing brace at line 40, execution continues at line 49.
Then at line 50, the end of function main() is reached.

So if you want something else to happen, you need to add code to make it happen.
Last edited on
Sounds like you missed the point of functions. When a function finishes, either by reaching a return or because there is no more code to execute, the program flow goes back to where that function was called from. It's that simple.


An aside on what to call functions:
What you call an "int" isn't an int. Where you say
from another int?
we would say "from a function". An int is a data type. It's not a function. What you call "the int main" is the function named main.
Last edited on
Topic archived. No new replies allowed.