Faisal Faisal - 4 months ago 19
C Question

How to jump between functions and main() in C programming

How can I go back to the main function from another sub function?
in C programming

in main() the user is asked whether he want games or calculator.
if he chooses games for example, he will be going to the function games
when he is in games function he can choose which game he wants or going back to
the main menu which shows games and calculator.

eg:

//prototypes
function one
function sub_one

main() {

select the function :
games:1
calculator:2
go to ?(function games)?: ....
}

////////////////////////////
function games {

select the game :
snake:1
painter:2
want to go back? yes? main()
}

////////////////////////////
function snake {
a+b .. get my work done here and i wanna goo back to games()
want to go back? yes? function games()
}


I succeeded to go back to previous functions except from the one which is pointed to in main().

I tried to define a global var and use it in a while loop inside main() in order to change
it from any function to be able to go back from any part of my code.

It seems pretty easy but I have lost my patience because I spent all my day trying
to do this thing and that's why I am seeking a little hint from you.

Thank you so much.

Answer

How you return to the main function depends upon how you declared your other functions:

  • if you declare the function void function (...), then you can simply return at any point, or allow control to run off the end of the function -- the compiler will automatically return to the calling function

  • if you declare the function to return a value int function(...) or char * function(...) then you must return foo; from your functions -- and foo's type must match the return type of the function. You cannot simply allow control to run off the end of the function.

Some examples:

#include <stdlib.h>
#include <stdio.h>

#define SNAKE 1
#define PAINTER 2

void play_snake() {
    /* play the snake game */
    return;
}

void play_painter() {
    /* sistine chapel time */
    return;
}

int prompt_for_choice() {
    char choice[10];
    puts("Please make a choice");
    puts("");
    puts("1 play snake");
    puts("2 play painter");
    fgets(choice, sizeof(choice), stdin);
    return strtol(choice, NULL, 10);
}

int main(int argc, char* argv[]) {
    int choice;

    choice = prompt_for_choice();

    if (choice == SNAKE) {
        play_snake();
    } else if (choice == PAINTER) {
        play_painter();
    } else {
        printf("internal error, invalid choice %d\n", choice);
        exit(1);
    }

    exit(0);
}

Note that there is nothing special about main. It is just another function.

I strongly recommend getting a good book about C. My favorite "first book" is The C Programming Language. Be sure to get the second edition, as the first edition describes an earlier version of the language. (The first edition might still be fun reading, but does not accurately represent the C language as it is used today.)