简体   繁体   English

从 main() 调用 function

[英]Calling a function in from main()

I've just started to learn C, and right now I find it a bit confusing how to call one function from another.我刚刚开始学习 C,现在我发现如何从另一个调用 function 有点令人困惑。 Here's my little project, I thought that writhing the line "int result = timeToWork;"这是我的小项目,我认为扭动“int result = timeToWork;”这一行would be enough to call that function, but the warning "initialization of 'int' from 'int(*)(int)' makes integer from pointer without a cast" appears.调用 function 就足够了,但是会出现警告“从 'int(*)(int)' 初始化 'int' 使 integer 从没有强制转换的指针中获取”。 The project compiles, but the result is some weird numbers instead of printing the line.该项目编译,但结果是一些奇怪的数字而不是打印该行。 What am I doing wrong?我究竟做错了什么?

#include<stdio.h>
int timeToWork(int input);
int main()
{
    printf("Please, enter the number  \n");
    int key = 0;
    fflush(stdout);
    scanf("%d", &key);
    int result = timeToWork;
    printf("Time = %d  \n",timeToWork);

    return 0;
}
int timeToWork(int input)
{
    if(input == 1)printf("It will take you 25 minutes to get to your destination by car  \n");
    else if(input == 2)printf("It will take you 20 minutes to get to your destination by bike  \n");
    else if(input == 3)printf("It will take you 35 minutes to get to your destination by bus  \n");
    else if(input == 4)printf("It will take you 30 minutes to get to your destination by train  \n");
    else printf("ERROR: please, enter a number from 1 to 4  \n");

    return 0;
}

The function has one parameter. function 有一个参数。

int timeToWork(int input);

So how are you going to call it without passing an argument to the function?那么如何在不向 function 传递参数的情况下调用它?

int result = timeToWork;

Even if a function does not accept an argument you have to specify parentheses after the function deisgnator.即使 function 不接受参数,您也必须在 function 设计器之后指定括号。

In the declaration above the function designator is just converted to pointer to the function在上面的声明中 function 指示符只是转换为指向 function 的指针

it looks like看起来像

int( *fp )( int ) = timeToWork;
int result = fp;

To call the function you should write要调用 function 你应该写

int result = timeToWork( key );

And instead of而不是

printf("Time = %d  \n",timeToWork);

it is evident you mean很明显你的意思是

printf("Time = %d  \n", result);

Though the function is defined incorrectly because it always returns 0.虽然 function 定义不正确,因为它总是返回 0。

I think the function should be declared and defined the following way我认为 function 应该按以下方式声明和定义

int timeToWork( int input )
{
    int time = 0;

    if(input == 1)
    {
        printf("It will take you 25 minutes to get to your destination by car  \n");
        time = 25;
    }
    else if(input == 2)
    {
        printf("It will take you 20 minutes to get to your destination by bike\n");
        time = 20;
    }
    else if(input == 3)
    {
        printf("It will take you 35 minutes to get to your destination by bus  \n");
        time = 35;
    }
    else if(input == 4)
    {
        printf("It will take you 30 minutes to get to your destination by train  \n");
        time = 30;
    }
    else
    {
         printf("ERROR: please, enter a number from 1 to 4  \n");
    }

    return time;
}

Your function timeToWork expects an input.您的 function timeToWork需要输入。

It should be int result = timeTowork(key);它应该是int result = timeTowork(key);

Also, what are trying to print here?: printf("Time = %d \n",timeToWork);另外,这里要打印什么?: printf("Time = %d \n",timeToWork);

You need to be sending in a parameter of type int as the function is expecting it.您需要发送 int 类型的参数,因为 function 期待它。

int result = timetowork(#your input goes here);

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

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