Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how is definition of a function after its use in a function call interpreted?

Tags:

c

Consider this code:

int main()
{
        int e;
        prn(e);
        return 0;
}

void prn(double x,int t)
{
}

Why does this code gives following warnings and no errors?

m.c:9: warning: conflicting types for ‘prn’  
m.c:5: note: previous implicit declaration of ‘prn’ was here

Shouldn't it give a "undefined function" error?

like image 787
Sandip Avatar asked Mar 11 '10 00:03

Sandip


People also ask

Can you define a function after calling it?

It is not possible. Python does not allow calling of a function before declaring it like C.

WHAT IS function and its use How do you define and call function?

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

What happens if you define a function but do not call it?

A common error is defining a function but forgetting to call the function. A function does not automatically get executed. A function that does not explicitly return a value returns the JavaScript value undefined.

Do you need to define a function before calling it?

To invoke (call) these functions they always need a variable name. This kind of function won't work if it is called before it has been defined which means Hoisting is not happening here. We must always define the expression function first and then invoke it.


2 Answers

In C99 it should give an undeclared function error.

In C89/90 declaring functions is not mandatory. If an undeclared function is called, the compiler will assume that it returns int and it will pass arguments to it after subjecting them to so called default argument promotions. In other words, the compiler will try to deduce what that function is from the actual call. If later the function is defined differently from what the compiler deduced, the behavior is undefined. Normally compilers will complain about it with a warning.

This is what you observe in your case. When the compiler sees the prn(e) call it assumes that prn is int prn(int) function. But later it discovers that it is actually a void prn(double, int). The mismatch is causing the warning.

In this case you are lucky in a sense that the call to undeclared function takes place in the same translation unit where the function is defined. So the compiler has a chance to compare the call and the definition and issue a warning about the conflict. If prn was defined in some other translation unit, the compiler would never have a chance to compare the two, so you'd have a full-fledged undefined behavior on your hands.

like image 61
AnT Avatar answered Sep 23 '22 19:09

AnT


C89 has implicit function declarations. If you use a function without declaring it, the compiler assumes it is int prn(). When this turns out to be wrong (because the return types differ), it's letting you know.

Note that the empty argument list in a function declaration means "parameters not specified", not "no parameters". It's then your responsibility to make sure that the input parameters match between the caller and the callee (which they don't, here), and you might get no help at all from the compiler with that.

In C99 this "feature" was removed. I think it was only ever really in C89 for compatibility with ancient C code. There was a time C didn't have function prototypes at all, you always just had to get it right on your own.

like image 34
Steve Jessop Avatar answered Sep 26 '22 19:09

Steve Jessop