Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function prototype
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Declaration of a function's name and type signature but not body}} {{About|function declarations|the prototype property of JavaScript functions and objects|JavaScript#Object-orientation (prototype-based)|other uses|Software prototyping}} {{more citations needed|date=September 2016}} In [[computer programming]], a '''function prototype''' is a [[Declaration (computer programming)|declaration]] of a [[function (programming)|function]] that specifies the function's name and [[type signature]] ([[arity]], [[data type]]s of [[parameter (computer programming)|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 (programming language)|C]] and [[C++]] where placing [[forward declaration]]s of functions in [[header file]]s allows for splitting a program into [[translation unit (programming)|translation unit]]s, i.e. into parts that a [[compiler]] can separately translate into [[object file]]s, to be combined by a [[linker (computing)|linker]] into an [[executable]] or a [[library (computing)|library]]. The function declaration precedes the function definition, giving details of name, return type, and storage class along with other relevant attributes.<ref>{{Cite web |last=TylerMSFT |date=2023-01-25 |title=Function Prototypes |url=https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170 |access-date=2023-08-09 |website=learn.microsoft.com |language=en-us}}</ref> Function prototypes can be used when either:<ref>{{Cite web |date=2018-10-25 |title=Function prototypes |url=https://www.ibm.com/docs/es/rbd/9.1.1.2?topic=functions-function-prototypes |access-date=2023-08-09 |website=www.ibm.com |language=en-us}}</ref> * Defining an ExternalType * Creating an Interface part In a prototype, parameter names are optional (and in C/C++ have function prototype [[Scope (computer science)|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 (computer programming)|pointer]] or a reference to {{mono|[[const]]}} parameter) except {{mono|const}} alone. In [[object-oriented programming]], [[Protocol (object-oriented programming)|interfaces]] and [[Method (computer programming)#Abstract methods|abstract methods]] serve much the same purpose. == Example == Consider the following function prototype: <syntaxhighlight lang="c"> void sum(int a, int b); </syntaxhighlight> or <syntaxhighlight lang="c"> void sum(int, int); </syntaxhighlight> or <syntaxhighlight lang="cpp"> auto sum(int, int) -> void; // C++ only </syntaxhighlight> 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 <code>int</code> 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. <syntaxhighlight lang="c"> char MyFunction(int a); /* Function prototype */ #include <stdio.h> #include <limits.h> int main(void) { putchar(MyFunction(-1)); /* Correctly formatted call */ putchar(MyFunction(1.5)); /* Compiler generates a warning because of type mismatch */ putchar(MyFunction("IncorrectArgType")); /* Compiler will generate a warning */ putchar(MyFunction()); /* Compiler will generate an Error too few arguments */ int one = 1; putchar(MyFunction(INT_MAX + one)); /* Although adding 1 to the maximum integer /* is an error it cannot be detected at compile time */ return 0; } char MyFunction(int n) /* Function definition */ { if (n > 0) return '>'; if (n < 0) return '<'; return '='; } </syntaxhighlight> The function {{mono|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 [[Include directive|header file]], one can specify an [[protocol (object-oriented programming)|interface]] for a [[Library (computing)|library]]. === Class declaration === In C++, function prototypes are also used in [[class (computer science)|class]] definitions. == See also == * [[Modular programming]] * [[Protocol (object-oriented programming)]] * [[Method (computer programming)#Abstract methods|Abstract method]] == References == {{Reflist}} {{refbegin|}} * {{cite book|title=The C Programming Language|last1=Kernighan|first1=Brian W.|author-link1=Brian Kernighan|last2=Ritchie Afree|first2=Dennis M.|author-link2=Dennis Ritchie|year=1988|publisher=Prentice Hall PTR|place=Upper Saddle River, NJ|edition=2nd|isbn=0-13-110362-8|url-access=registration|url=https://archive.org/details/cprogramminglang00bria}} {{refend}} [[Category:Subroutines]] [[Category:C programming language family]] [[Category:Computer programming]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Mono
(
edit
)
Template:More citations needed
(
edit
)
Template:Refbegin
(
edit
)
Template:Refend
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)