This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Function prototype" – news ·newspapers ·books ·scholar ·JSTOR(September 2016) (Learn how and when to remove this message) |
Incomputer programming, afunction prototype is adeclaration of afunction that specifies the function's name andtype signature (arity,data types ofparameters, andreturn type), but omits the function body. While a function definition specifieshow the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e.what data types go in and come out of it. The term "function prototype" is particularly used in the context of the programming languagesC andC++ where placingforward declarations of functions inheader files allows for splitting a program intotranslation units, i.e. into parts that acompiler can separately translate intoobject files, to be combined by alinker into anexecutable or alibrary. The function declaration precedes the function definition, giving details of name, return type, and storage class along with other relevant attributes.[1]
Function prototypes can be used when either:[2]
In a prototype, parameter names are optional (and in C/C++ have function prototypescope, meaning their scope ends at the end of the prototype), however, the type is necessary along with all modifiers (e.g. if it is apointer or a reference toconst parameter) exceptconst alone.
Inobject-oriented programming,interfaces andabstract methods serve much the same purpose.
Consider the following function prototype:
voidsum(inta,intb);
or, without named parameters:
voidsum(int,int);
or, with trailing return types (C++ only):
autosum(int,int)->void;
Function prototypes include the function signature, the name of the function, return type and access specifier. In this case the name of the function is "sum". The function signature defines the number of parameters and their types. The return type is "void". This means that the function is not going to return any value. Note that the parameter names in the first example are optional.
In early versions of C, if a function was not previously declared and its name occurred in an expression followed by a left parenthesis, it was implicitly declared as a function that returns anint and nothing was assumed about its arguments. In this case the compiler would not be able to perform compile-time validity checking of the number and type(s) of arguments. TheC99 standard requires the use of prototypes.
#include<limits.h>#include<stdio.h>// Function prototypecharmyFunction(inta);intmain(void){putchar(myFunction(-1));// Correctly formatted callputchar(myFunction(1.5));// Compiler warning: type mismatchputchar(myFunction("IncorrectArgType"));// Compiler warning: type mismatchputchar(myFunction());// Compiler error: too few arguments// Although adding 1 to INT_MAX is an integer overflow error,// it cannot be detected at compile timeputchar(myFunction(INT_MAX+1));return0;}// Function definitioncharmyFunction(intn){if(n>0){return'>';}elseif(n<0){return'<';}else{return'=';}}
The functionMyFunction expects to be called with an integer argument. By including the function prototype, you inform the compiler that the function takes one integer argument and you enable the compiler to catch incorrectly specified calls.
By placing function prototypes in aheader file, one can specify aninterface for alibrary.
In C++, function prototypes are also used inclass definitions.