Function prototype

As a function prototype is defined in different programming languages ​​(especially C and C ) the declaration of a function - including information on the number and type of parameters and the return value type - separated from its implementation (definition). One also speaks inaccurately of the forward declaration (English: forward declaration, often misspelled as " forward declaration " translated ) of a function, but this does not represent a full function prototypes in each case. Example: int function ( ); would be made into a valid C forward declaration, but no prototype, since no information on function parameters. Each definition of a function, however, automatically delivers always a prototype declaration for the following program code.

Background

With the help of function prototypes the compiler is the interface of a function informed before using it. Thus, the compiler can check at each function call if the function parameters and the return value will be used consistently. The full implementation of a function can thus take place at a later date or in a separate program module. This concept also allows the reciprocal call between two or more functions (English: mutual recursion ), and the use of single-pass compilers that translate the source code of a program in a single pass. In the design of Pascal Niklaus Wirth took advantage of those already known from Algol advance declarations to allow the implementation of a single-pass compiler at that stage in the compiler technology.

Use

Function prototypes can be used only to provide the interface information available and to keep hidden from the internal realization. In this case, function prototypes support the so-called information hiding. Function prototypes and associated implementations are then kept separate. Published Only the files with the function prototypes. In this form, function prototypes were used as in the language Modula -2. In the Object Pascal language you can control whether external objects are permitted to access internals or not by entering prototypes in a public and a protected part. This also serves the secret principle. While in the first case there is a possibility to hide internals of the concept in the second case the suppression of access is more likely to internals.

While must be used, for example, in C function prototypes, this is mandatory only in certain cases in C:

  • Functions in the C standard library may not be used without prototypes.
  • For functions at their call, the automatic adaptation of the arguments (default argument promotion) to the corresponding parameters would provide inappropriate data types, function prototypes are also mandatory.

In various directives, such as MISRA -C, the use of function prototypes for consistency check is required. In the C99 no function prototype performing simple forward declaration of a function is already being called outdated, indicating a removal of this variant in future versions of the language standard.

Example in C

/ / Contains among other things the function prototype for printf (): # include   / / Prototype declaration, the parameter identifier are optional: double sum ( double number1, double number2 );   int main ( void) {      / / Call the function; no function prototype would be here      / / Argument type (int ) and parameter type (double) incompatible:      printf ("2 3 =% g \ n", sum (2, 3) );      return 0; }   / / Definition of the function: double sum ( double number1, double number2 ) {      return num1 num2; } References

355986
de