C 编程中如何在函数和 main() 之间跳转

发布于 2024-12-11 20:15:20 字数 772 浏览 0 评论 0原文

如何从另一个子函数返回主函数? 在 C 编程中

,main() 中会询问用户是否想要游戏或计算器。 例如,如果他选择游戏,他会选择功能游戏 当他处于游戏功能时,他可以选择他想要的游戏或返回 主菜单显示游戏和计算器。

例如:

//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()  
}

我成功地返回到之前的函数,除了 main() 中指向的函数之外。

我尝试定义一个全局变量并在 main() 内的 while 循环中使用它以进行更改 它可以从任何函数中返回,以便能够从我的代码的任何部分返回。

这看起来很容易,但我已经失去了耐心,因为我花了一整天的时间去尝试 做这件事,这就是为什么我寻求你的一点提示。

太感谢了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2024-12-18 20:15:20

您可以通过从 main() 最近调用的函数返回来返回主函数。请注意,您不调用 main(),您只需从函数返回到 main。执行函数的最后一条语句后,下一条语句是调用它的函数中的下一条语句。

我认为您实际上想使用某些状态变量来控制执行哪个函数:

#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;
}

输出:

$ ./foo 
f()
h()
g()

更干净的是,您可以通过在 f()< 末尾返回新状态来从全局状态变量切换到局部状态变量/code>、g()h() 函数。

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.

执着的年纪 2024-12-18 20:15:20

如何返回 main 函数取决于您如何声明其他函数:

  • 如果您声明函数 void function (...),那么您可以简单地在任何时候return,或者允许控制在函数末尾运行——

  • 如果您声明函数返回一个值 int function(...),编译器将自动返回到调用函数

    char * function(...) 那么你必须从你的函数中return foo;——并且foo的类型必须与函数的返回类型匹配。您不能简单地允许控制在函数末尾运行。

一些示例:

#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);
}

请注意,main 没有什么特别的。这只是另一个功能。

我强烈推荐一本关于 C 语言的好书。我最喜欢的“第一本书”是 C 编程语言 。请务必获得第二版,因为第一版描述了该语言的早期版本。 (第一版读起来可能仍然很有趣,但并不能准确地代表当今使用的 C 语言。)

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.)

余生再见 2024-12-18 20:15:20

在你的主函数中,只需调用一个菜单函数来调用所有子菜单,当在子菜单中时,你可以回调主函数中的菜单函数。也是新手,但希望这有帮助

#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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文