Why use function prototypes in C? It seems sort of redundant because we already declare the function name, argument types, and return type in the definition. Do the prototypes have to be declared before the function is defined or used for the optimizations?
A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
It is not required, but it is bad practice not to use prototypes. With prototypes, the compiler can verify you are calling the function correctly (using the right number and type of parameters).
Hence, C++ does NOT require functions to be declared/prototyped.
Here we will see what are the purpose of using function prototypes in C or C++. The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function.
While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface. Creating library interfaces: By placing function prototypes in a header file, one can specify an interface for a library. Show activity on this post.
A lot of its design choices exist to make compilers easier to write given the computer limitations of the time. Prototypes are one such example. The main reason they exist is so that the compiler can go through the program exactly once, knowing which functions are in scope at any given time.
Up to the C90 standard, C compilers assumed the return type of the omitted function prototype as int. And this assumption at the compiler side may lead to unspecified program behavior.
Generally speaking, you don't need to explicitly declare functions because defining them also declares them. Here are two situations where you would need to:
The definition of the function is in an external module.
For example, if the function is defined in definer.c
, but you want to call it from user.c
, you will need to declare the function in user.c
or a file included by it (typically, definer.h
).
The definition of the function comes after a call to it.
For example, if you have two functions that call each other, you will need to declare the second one before the definition of the first one.
While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.
Creating library interfaces: By placing function prototypes in a header file, one can specify an interface for a library.With the declaration of a function the compiler can check the consistent use of parameters and return value, and can compile the code even if the function is not implemented in this module. If the function is only declared but not implemented in the respective module, this gap will be closed by the linker, not the compiler.
It's similar to declaring extern variables. If you'd define them, the memory for them would be allocated multiple times. That's why you should never define variables in h-files, but declare them there. Including the h-file would result in multiple allocations of memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With