Function prototype

Print Print
Reading time 4:33

In computer programming, a function prototype or function interface is a declaration of a function that specifies the function’s name and type signature (arity, data types of parameters, and return type), but omits the function body. While a function definition specifies how 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 languages C and C++ where placing forward declarations of functions in header files allows for splitting a program into translation units, i.e. into parts that a compiler can separately translate into object files, to be combined by a linker into an executable or a library.

In a prototype, parameter names are optional (and in C/C++ have function prototype scope, meaning their scope ends 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 parameter).

In object-oriented programming, interfaces and abstract methods serve much the same purpose.

Example

Consider the following function prototype:

void Sum( int a, int b );

OR

void Sum( int, int );

First of all, 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 determines the number of parameters and their types. In the above example, 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.

Uses

In earlier 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 an int and nothing was assumed about its arguments. In this case the compiler would not be able to perform compile-time checking of argument types and Syntax arity when the function was 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
 * |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

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

Class declaration

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

See also

References

  • Kernighan, Brian W.; Ritchie Afree, Dennis M. (1988). The C Programming Language (2nd ed.). Upper Saddle River, NJ: Prentice Hall PTR. ISBN 0-13-110362-8.

By: Wikipedia.org
Edited: 2021-06-18 14:10:01
Source: Wikipedia.org