简体   繁体   English

嵌套函数或函数中的调用函数

[英]Nested function or calling function in a function

So, I'm not sure how to start, so I'll start with some code: 因此,我不确定如何开始,所以我将从一些代码开始:

    int main()
{
    system("cls");
    printf( "1. D4\n" );
    printf( "2. D6\n" );
    printf( "3. D8\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:            
            roll_d4();
            break;

First off, I have this, and when I pick a selection, it works to get me to the function I wanted. 首先,我有这个,当我选择一个选项时,它可以使我进入想要的功能。 So when I get to the D4 function, I have this: 因此,当我进入D4功能时,我有以下内容:

void roll_d4() {
system("cls");
printf("How many D4 do you want to roll?\n");
printf("Enter a number 1-20");
printf("\nSelection: ");
scanf("%d", &input);
switch (input){
    case 5:
    exit(0);
    d4_number();
}

Now, every time I try to run this, it doesn't like the d4_number(); 现在,每次我尝试运行此命令时,它都不喜欢d4_number(); and says it's an implicit declaration. 并说这是一个隐式声明。 I'm just trying to let you pick a selection and have it go to the next part in the code, in this case a function that will actually roll a die rather than being a menu like this one. 我只是想让您选择一个选项并将其转到代码的下一部分,在这种情况下,该函数实际上将滚动骰子,而不是像这样的菜单。 But doing it this way isn't working, and I'm at a loss at what to do. 但是以这种方式这样做是行不通的,而且我对如何做一无所知。

Edit: Code addition. 编辑:代码添加。 Here's the full program: 这是完整的程序:

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include<string.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
char response;
    int sum = 0;
    time_t t;
    int result;

    int die_d4_1 = 0;
    int die_d4_2 = 0;
    int die_d4_3 = 0;
    int die_d4_4 = 0;
    int die_d4_5 = 0;
    int input;

void roll_d4() {
    system("cls");
    printf("How many D4 do you want to roll?\n");
    printf("Enter a number 1-20");
    printf("\nSelection: ");
    scanf("%d", &input);
    switch (input){
        case 5:
        exit(0);
        d4_number(0);
    }
}
void roll_d6()
{
    printf( "How many D6 do you want to roll?" );
}
void roll_d8()
{
    printf( "How many D8 do you want to roll?" );
}

void d4_number(){
    printf("How many d4 do you want to roll?");
}

int main()
{
    system("cls");
    printf( "1. D4\n" );
    printf( "2. D6\n" );
    printf( "3. D8\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:            /* Note the colon, not a semicolon */
            roll_d4();
            break;
        case 2:          
            roll_d6();
            break;
        case 3:         
            roll_d8();
            break;
        case 4:        
            printf( "Thanks for playing!\n" );
            break;
        default:            
            printf( "Bad input, quitting!\n" );
            break;
    }
    getchar();

}

It's obviously very incomplete, but I'm just trying to solve this problem before adding more. 这显然是很不完整的,但是我只是想在添加更多内容之前解决此问题。

You need to give d4_number() a prototype, before the call, so the compiler know what arguments it takes and what it returns. 您需要在调用之前给d4_number()一个原型,以便编译器知道它需要什么参数以及它返回什么。 Otherwise it still links (for historical reasons) and assumes it returns an int. 否则,它仍然会链接(由于历史原因),并假定它返回一个int值。

If you take a closer look at this code 如果您仔细看一下这段代码

switch (input){
    case 5:
    exit(0);
    d4_number(0);
}

you can notice that before d4_number function there is an exit function invoked. 您会注意到,在d4_number函数之前,有一个exit函数被调用。 In that case d4_number function has never a chance to execute because the whole program stops and exits with 0 as a return code. 在那种情况下, d4_number函数将永远无法执行,因为整个程序都会停止并以0作为返回码退出。

Other issue is that d4_number function definition does not accept any arguments, but in statement d4_number(0) there is one extra parameter passed. 另一个问题是d4_number函数定义不接受任何参数,但是在语句d4_number(0)传递了一个额外的参数。

Okay, I feel like an idiot. 好吧,我觉得自己是个白痴。 All I had to do was move the declaration: 我要做的就是移动声明:

        void d4_number(){
    printf("How many d4 do you want to roll?");
}

above 以上

void roll_d4() {
system("cls");
printf("How many D4 do you want to roll?\n");
printf("Enter a number 1-20");
printf("\nSelection: ");
scanf("%d", &input);
switch (input){
    case 5:
    d4_number(0);
}

so that it was declared before calling...I feel really dumb for this, but thanks everyone for the quick responses! 所以在打电话之前就宣布了...我为此感到很愚蠢,但是感谢大家的迅速回复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM