Functional prototype

from Wikipedia, the free encyclopedia

In various programming languages (especially C and C ++ ), a function prototype or function head is the declaration of a function - including information on the number and type of parameters and the type of return value - separately from its implementation (definition). This is also called inaccurate by the forward declaration (English: forward declaration , often mistranslated as "forward declaration") of a function, but this does not in any case represent a valuable function prototypes. Example: would be a valid advance declaration in C, but not a prototype, since no information about function parameters is made. Each definition of a function, however, always automatically provides a prototype declaration for subsequent program code. int funktion();

background

With the help of function prototypes, the compiler is informed of the interface of a function before it is used. In this way, the compiler can check each time the function is called whether the function parameters and the return value are used consistently. A function can be fully implemented at a later point in time or in a separate program module. This concept also enables two or more functions to be called alternately ( mutual recursion ) and the use of single-pass compilers that translate the source code of a program in one go. When designing Pascal , Niklaus Wirth used such advance declarations, which were already known from ALGOL , in order to enable the implementation of a single-pass compiler with the current state of compiler technology.

use

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

While z. If, for example, function prototypes have to be used in C ++, this is only mandatory in C in certain cases:

  • Functions of the C standard library must not be used without a prototype.
  • Function prototypes are also mandatory for functions which, when called, would result in the automatic adjustment of the arguments (default argument promotion) for the corresponding parameters.

Various guidelines, for example MISRA-C , require the use of function prototypes for checking consistency. In C99 , the simple advance declaration of a function, which does not represent a function prototype , is already referred to as obsolete, which indicates that this variant will be removed in future versions of the language standard.

Example in C

// enthält unter anderem den Funktionsprototypen für printf():
#include <stdio.h>

// Prototypdeklaration, die Parameterbezeichner sind optional:
double summe( double zahl1, double zahl2 );

int main( void )
{
    // Aufruf der Funktion; ohne Funktionsprototyp wären hier
    // Argumenttyp (int) und Parametertyp (double) inkompatibel:
    printf( "2+3=%g\n", summe( 2, 3 ) );
    return 0;
}

// Definition der Funktion:
double summe( double zahl1, double zahl2 )
{
    return zahl1 + zahl2;
}

See also

literature

  • British Standards Institute (Ed.): The C Standard - Incorporating TC1 - BS ISO / IEC 9899: 1999 . John Wiley & Sons, 2003, ISBN 0-470-84573-2 .

Individual evidence

  1. ^ Brian W. Kernighan: Why Pascal is Not My Favorite Programming Language . 1981 (English, liu.se [PDF; accessed on September 21, 2010]).
  2. ^ Working Draft, Standard for Programming Language C ++. (PDF; 4.6 MB) (No longer available online.) Formerly in the original ; accessed on September 26, 2010 (English, non-normative working document).  ( Page no longer available , search in web archivesInfo: The link was automatically marked as defective. Please check the link according to the instructions and then remove this notice.@1@ 2Template: Dead Link / www.open-std.org  
  3. ^ MISRA C Rules. (PDF) (No longer available online.) Archived from the original on February 22, 2014 ; accessed on February 7, 2014 (English). Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / home.sogang.ac.kr
  4. ^ Rationale for C99, Revision 5.10. (PDF; 898 kB) Retrieved on September 12, 2010 (English, Section 6.11.6).