C prototype functions

9.2k views Asked by At

As a beginner to C, I can understand the need for function prototypes in the file, but am unsure of a couple things.

First, does every function call outside of the main require a prototype declaration? Are there any conditions where that can change?

Second, do you need a separate function prototype for method overloads?

4

There are 4 answers

0
CB Bailey On BEST ANSWER

Function calls in C don't require a prototype to be visible but it is highly recommended that a correct prototype is in scope.

The reason for this is that if the function definition doesn't match the types of the function arguments after the default function argument promotions are performed you are highly likely to get undefined behavior.

Having the correct prototype visible means that the compiler can check the arguments of a function call and warn the programmer if there is a mismatch.

C doesn't allow functions to be overloaded so you can only have a single prototype for any function name.

Default argument promotions can cause unexpected mismatches.

E.g.

int main(int argc, char **argv)
{
    short s = 5;
    float f = 2.3f;
    x(s, f);     // x implicitly declared; default argument promotions performed
    return 0;
}

int x(short t, float g)  // Error: called with an int and a double
{
    return (int)(t + g);
}

In the function call, because x has no visible prototype (yet), s will be promoted to int and f will be promoted to double. These are default argument promotions. This then causes a mismatch when the function is defined with a prototype that takes a short and a float even though these are the original types of the arguments that were passed in.

Functions that take a variable number of arguments (i.e. use , ... syntax) must always have a visible prototype at the point at which they are called.

1
AudioBubble On

No, functions are not required to have prototypes. However, they are useful as they allow functions to be called before they are declared. For example:

int main(int argc, char **argv)
{
    function_x(); // undefined function error
    return 0;
}

void function_x()
{

}

If you add a prototype above main (this is usually done in a header file) it would allow you to call function_x, even though it's defined after main. This is why when you are using an external library that needs to be linked, you include the header file with all the function prototypes in it.

C does not have function overloading, so this is irrelevant.

1
Outspoken On

No. In C not every function requires a prototype. BUT it is good practice to include it. Prototypes help the compiler catch errors. Of course some C compilers, by default complain if prototypes are missing before the function call, but this behavior is not necessarily a standard.

The above logic applies to overloads too.

Of-course, since you are new to C. I think you better go and look up the difference between a Prototype and Definition of a function to make full sense.

0
Jerry Coffin On

Others have already pointed out that C doesn't require prototypes for functions. I'll just add a couple minor points.

First of all, without a prototype, the arguments to a function always undergo "default promotions" before being passed as parameters, so (for example) all the smaller integer types get promoted to int, and float gets promoted to double. Therefore, without a prototype, it's impossible for a function to receive (for example) a char, short, or float. If the function is defined to receive one of those types, there will be a mismatch between what's passed and what the function expects, giving undefined behavior (any other mismatch gives undefined behavior as well).

Second, if a function is defined with a variable argument list, then you do need a prototype for it, or else calling it will result in undefined behavior.