Reputation:
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.
Upvotes: 2
Views: 29186
Reputation: 1
In your main function, just call a menu function that call all sub menus and when in the sub-menu, you can call back the menu function which lies in the main function. Also a newbie but hope this helps
#include<stdio.h>
int menu();
int games();
int calculator();
int main(){//main function starts
menu();
return 0;
}
int menu(){
int choice;
printf("Select Function\n");
printf("1. Games\n");
printf("2. Calculator\n");
printf("3. Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
games();
break;
case 2:
calculator();
break;
case 3:
return 1;
break;
default:
printf("invalid Choice\n");
return 0;
}
}
int games(){
int choice2;
printf("Select Game\n");
printf("1. Snake\n");
printf("2. Painter\n");
printf("3. Go back to main Menu\n");
scanf("%d",&choice2);
switch(choice2){
case 1:
printf("Call function for Snake\n");
;break;
case 2:
printf("Call function for Painter\n");
break;
case 3:
menu();
default:
printf("invalid Choice\n");
}
return 0;
}
int calculator(){
//code goes here
}
Upvotes: 0
Reputation: 104110
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.)
Upvotes: 1
Reputation: 7336
You can get back to the main function by returning from the function most recently called by main()
. Note that you don't call main()
, you simply return from your function to main. After the last statement of a function is executed, the next statement is the next one in the function that called it.
I think you actually want to control which function gets executed using some a state variable:
#include <stdio.h>
#include <stdlib.h>
enum {ff, gg, hh} state = ff;
void f();
void g();
void h();
int main(void)
{
while(1)
{
switch (state)
{
case ff:
f();
break;
case gg:
g();
break;
case hh:
h();
break;
}
}
}
void f()
{
printf("f()\n");
state = hh;
}
void g()
{
printf("g()\n");
exit(0);
}
void h()
{
printf("h()\n");
state = gg;
}
Output:
$ ./foo
f()
h()
g()
More cleanly, you could switch from a global to a local state variable by returning the new state at the end of the f()
, g()
and h()
functions.
Upvotes: 4