-3

I've just started to learn C, and right now I find it a bit confusing how to call one function from another. Here's my little project, I thought that writhing the line "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. 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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    "I've just started to learn C" - what resources are you using for this learning? Have these resources covered how to call functions yet? If yes, please indicate what they say and why it's unclear to you. If not, perhaps you should follow the resource until it does. For such very basic things, learning from a training resource is probably better than trying to piece together your answers from Stack Overflow. – Angew is no longer proud of SO Oct 07 '19 at 14:05
  • 1
    Whatever source you're learning C from should have a section on calling functions - why not refer to that ? – Sander De Dycker Oct 07 '19 at 14:05
  • 1
    You might want to actually call the function: `int result = timeToWork(key)`. – Marco Bonelli Oct 07 '19 at 14:05
  • You should format your code like the samples in your beginner's C text book or whatever resource you're learnin C from. This very important. – Jabberwocky Oct 07 '19 at 14:08
  • BTW: the warning _"initialization of 'int' from 'int(*)(int)' makes integer from pointer without a cast"_ is almost always an error. And most other warnings (if not all) should be considered as errors. – Jabberwocky Oct 07 '19 at 14:09

3 Answers3

3

The function has one parameter.

int timeToWork(int input);

So how are you going to call it without passing an argument to the function?

int result = timeToWork;

Even if a function does not accept an argument you have to specify parentheses after the function deisgnator.

In the declaration above the function designator is just converted to pointer to the function

it looks like

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

To call the function you should write

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.

I think the function should be declared and defined the following way

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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • the first sentences sound a little bit like "`int result = timeToWork;` would be ok if `timeToWork()` didn't expect a parameter". Maybe you want to clarify that – Ingo Leonhardt Oct 07 '19 at 14:11
  • How about `time = input*5+20`, `It will take you %d minutes`, and `if((unsigned)input-1<4) { /* do stuff */ }`? – S.S. Anne Oct 07 '19 at 15:00
0

Your function timeToWork expects an input.

It should be int result = timeTowork(key);

Also, what are trying to print here?: printf("Time = %d \n",timeToWork);

Abhijit
  • 468
  • 8
  • 22
-1

You need to be sending in a parameter of type int as the function is expecting it.

int result = timetowork(#your input goes here);
abelenky
  • 63,815
  • 23
  • 109
  • 159