Function prototype

From Wikipedia, the free encyclopedia
Jump to: navigation, search
This article is about function declarations. For the prototype property of JavaScript functions and objects, see JavaScript#Prototype-based. For other uses, see Prototype (computer science).

A function prototype or function interface in C, Perl, PHP or C++ is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types. While a function definition specifies HOW the function does what it does, a function prototype merely specifies its interface, i.e. WHAT data types go in and come out of it.

In a prototype, argument names are optional (and have function prototype scope, meaning they go out of scope at the end of the prototype), however, the type is necessary along with all modifiers (e.g. if it is a pointer or a const argument).

Example[edit]

Consider the following function prototype:

int myfunction(int n);

This prototype specifies that in this program, there is a function named "myfunction" which takes a single integer argument "n" and returns an integer. Elsewhere in the program a function definition must be provided if one wishes to use this function. It's important to be aware that a declaration of a function does not need to include a prototype. The following is a prototype-less function declaration, which just declares the function name and its return type, but doesn't tell what parameter types the definition expects.

double myfunction();

Uses[edit]

In earlier versions of C, if a function is not previously declared and its name occurred in an expression followed by a left parenthesis, it is implicitly declared as a function that returns an int and nothing was assumed about its arguments. In this case the compiler will not be able to perform compile-time checking of argument types and arity when the function is applied to some arguments. This can cause problems. The following code illustrates a situation in which the behavior of an implicitly declared function is undefined.

 #include <stdio.h>
 
 /* If this prototype is provided, the compiler will catch the error in
  * int main(). If it is omitted, then the error may go unnoticed.
  */
 int myfunction(int n);              /* Prototype */
 
 int main(void) {             /* Calling function */
     printf("%d\n", myfunction());   /* Error: forgot argument to myfunction */
     return 0;
 }
 
 int myfunction(int n) {             /* Called function definition */
     if (n == 0) 
         return 1;
     else 
         return n * myfunction(n - 1);
 }

The function myfunction expects an integer argument to be on the stack or in a register when it is called. If the prototype is omitted, the compiler will have no way of enforcing this and myfunction will end up operating on some other datum on the stack (possibly a return address or the value of a variable that is currently not in scope). By including the function prototype, you inform the compiler that the function myfunction takes one integer argument and you enable the compiler to catch these kinds of errors and make the compilation process run smoothly. This feature was removed from the C99 standard, thus omission of a function prototype will result in a compile error.

Creating library interfaces[edit]

By placing function prototypes in a header file, one can specify an interface for a library.

Class declaration[edit]

In C++, function prototypes are also used in class definitions.

See also[edit]

References[edit]