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.
How you return to the
mainfunction depends upon how you declared your other functions:if you declare the function
void function (...), then you can simplyreturnat any point, or allow control to run off the end of the function — the compiler will automatically return to the calling functionif you declare the function to return a value
int function(...)orchar * function(...)then you mustreturn foo;from your functions — andfoo‘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:
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.)