Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use function prototypes?

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?

like image 663
George Newton Avatar asked Feb 10 '14 06:02

George Newton


People also ask

What is the use of function prototype in C?

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.

Do you always need a function prototype?

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).

Is function prototype important in C++?

Hence, C++ does NOT require functions to be declared/prototyped.

What is the purpose of using function prototypes in C/C++?

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.

What is the difference between a function definition and function prototype?

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.

What is the purpose of a compiler prototype?

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.

What is the return type of omitted function prototype in C?

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.


3 Answers

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:

  1. 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).

  2. 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.

like image 118
ikegami Avatar answered Jan 01 '23 00:01

ikegami


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.
like image 33
Zhen Zhang Avatar answered Dec 31 '22 22:12

Zhen Zhang


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.

like image 29
Lord_Gestalter Avatar answered Dec 31 '22 22:12

Lord_Gestalter