Function prototype

Short description: Declaration of a function's name and type signature but not body

This article is about function declarations. For the prototype property of JavaScript functions and objects, see JavaScript § Object-orientation (prototype-based). For other uses, see Software prototyping.

In computer programming, a function prototype 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. 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]

* Defining an ExternalType
* Creating an Interface part

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 reference to const parameter) except const alone.

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

## Contents

* 1 Example
* 2 Uses
* 2.1 Creating library interfaces
* 2.2 Class declaration
* 3 See also
* 4 References

## Example

Consider the following function prototype:

void sum(int a, int b);

or, without named parameters:

void sum(int, int);

or, with trailing return types (C++ only):

auto sum(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.

## Uses

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 an `int` 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. The C99 standard requires the use of prototypes.

#include <limits.h>
#include <stdio.h>

// Function prototype
char myFunction(int a);

int main(void) {
putchar(myFunction(-1)); // Correctly formatted call
putchar(myFunction(1.5)); // Compiler warning: type mismatch
putchar(myFunction("IncorrectArgType")); // Compiler warning: type mismatch
putchar(myFunction()); // Compiler error: too few arguments

// Although adding 1 to INT_MAX is an integer overflow error,
// it cannot be detected at compile time
putchar(myFunction(INT_MAX + 1));

return 0;

// Function definition
char myFunction(int n) {
if (n > 0) {
return '>';
} else if (n < 0) {
return '<';
} else {
return '=';

The function MyFunction 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.

### 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

* Modular programming
* Protocol (object-oriented programming)
* Abstract method

## References

1. ↑ TylerMSFT (2023-01-25). "Function Prototypes" (in en-us). https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170.
2. ↑ "Function prototypes" (in en-us). 2018-10-25. https://www.ibm.com/docs/es/rbd/9.1.1.2?topic=functions-function-prototypes.

* 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. https://archive.org/details/cprogramminglang00bria.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Function prototype. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Function_prototype&oldid=4149364"