MNY
MNY

Reputation: 1536

How to use a function of one type, in a function of another type?

I have one function that getting printing a menue and returning a choice. And another function to calculate 2 numbers that im getting from the user. Now, the calculation is depanding on the choice return from the first function, and im dont really know what is the right way to use 2 different type of functions together...this is both functions:

float calc(float number1, float number2)

{
    float answer;
    int operand;
    operand =get_choice();// problemmmm

}

char get_choice(void)

{
    char choice;

    printf("Enter the operation of your choice:\n");
    printf("a. add        s. subtract\n");
    printf("m. multiply   d. divide\n");
    printf("q. quit");

    while ((choice = getchar()) != 'q')
    {

        if (choice != 'a' || choice != 's' || choice != 'm' || choice != 'd')
        {
            printf("Enter the operation of your choice:\n");
            printf("a. add        s. subtract\n");
            printf("m. multiply   d. divide\n");
            printf("q. quit");
            continue;
        }
    }
    return choice;
}

I got an error saying "implicit function of get_choice is invalid in C99"

Upvotes: 2

Views: 86

Answers (4)

hugomg
hugomg

Reputation: 69924

That implicit function error probably means that the compiler doesn't yet know about the get_choice function by the time it reaches the line when it gets called. You can fix this by either.

  1. Changing the order you write your functions in. Write get_choice before the calc function

  2. Add a declaration for the get_choice function before the calc function. The declaration would be just the function name and type, without the code:

    char get_choice(void);
    

If you are wondering what the message was about, in C89 undeclared functions were implicitly assumed to return an integer. C99 is stricter and forces you to always declare functions but the error message still refers to the C89 style implicit declarations.

Upvotes: 1

sr01853
sr01853

Reputation: 6121

I C/C++, It is required for a compiler to know the type (size) of an identifier, but not a particular value it holds (in case of variables). This is called forward declaration

In your case, the compiler doesn't know get_choice() when you call it from calc().

Upvotes: 0

John Bode
John Bode

Reputation: 123458

I got an error saying "implicit function of get_choice is invalid in C99"

In C, you need to declare a function before you call it; otherwise the compiler doesn't know what the return type is supposed to be.

You can do one of two things. One, you can declare the get_choice function before you call it:

float calc(float number1, float number2)
{
    float answer;
    int operand;

    char get_choice(void);

    operand =get_choice();// problemmmm
}

Alternately, you can switch the order in which the functions are defined, so that the get_choice function is defined before it's called (my preference).

Upvotes: 0

Hui Zheng
Hui Zheng

Reputation: 10224

Are you trying to perform different calculation depending on the operator? You can use function pointer to achieve it. Namely, write a dictionary that maps each choice('a', 's', 'm', 'd') respectively to operation(add, subtract, multiply, divide) of a function pointer type float (*)(float, float) .

BTW, if you haven't declared get_choice, you should put its function body BEFORE calc. The other problem is get_choice(void) return char, but you declare operand as int.

Upvotes: 0

Related Questions