Function prototype
Encyclopedia
A function prototype
Prototype
A prototype is an early sample or model built to test a concept or process or to act as a thing to be replicated or learned from.The word prototype derives from the Greek πρωτότυπον , "primitive form", neutral of πρωτότυπος , "original, primitive", from πρῶτος , "first" and τύπος ,...

in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 or C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 is a declaration of a function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 that omits the function body but does specify the function's return type
Return type
In computer programming, the return type defines and constrains the data type of value returned from a method or function...

, name, arity
Arity
In logic, mathematics, and computer science, the arity of a function or operation is the number of arguments or operands that the function takes. The arity of a relation is the dimension of the domain in the corresponding Cartesian product...

 and argument types. While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.

In a prototype, argument names are optional, however, the type is necessary along with all modifiers (i.e. If it is a pointer or a const argument
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...

).

Example

Consider the following function prototype:

int fac(int n);

This prototype specifies that in this program, there is a function named "fac" which takes a single integer
Integer
The integers are formed by the natural numbers together with the negatives of the non-zero natural numbers .They are known as Positive and Negative Integers respectively...

 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.

int fac;

Uses

In C, if a function is not previously declared and its name occurs in an expression followed by a left parenthesis, it is implicitly declared as a function that returns an int and nothing is assumed about its arguments. In this case the compiler will not be able to perform compile-time checking of argument types and arity
Arity
In logic, mathematics, and computer science, the arity of a function or operation is the number of arguments or operands that the function takes. The arity of a relation is the dimension of the domain in the corresponding Cartesian product...

 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

/*
* If this prototype is provided, the compiler will catch the error
* in main. If it is omitted, then the error may go unnoticed.
*/
int fac(int n); /* Prototype */

int main(void) { /* Calling function */
printf("%d\n", fac); /* ERROR: fac is missing an argument! */
return 0;
}

int fac(int n) { /* Called function */
if (n 0)
return 1;
else
return n * fac(n - 1);
}


The function "fac" expects an integer argument to be on the stack
Call stack
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack"...

 or in a register
Processor register
In computer architecture, a processor register is a small amount of storage available as part of a CPU or other digital processor. Such registers are addressed by mechanisms other than main memory and can be accessed more quickly...

 when it is called. If the prototype is omitted, the compiler will have no way of enforcing this and "fac" will end up operating on some other datum on the stack (possibly a return address
Return address
In postal mail, a return address is an explicit inclusion of the address of the person sending the message. It provides the recipient with a means to determine how to respond to the sender of the message if needed....

 or the value of a variable that is currently not in scope
Scope (programming)
In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them—or semantics...

). By including the function prototype, you inform the compiler that the function "fac" takes one integer argument and you enable the compiler to catch these kinds of errors.

Creating library interfaces

By placing function prototypes in a header file
Header file
Some programming languages use header files. These files allow programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers...

, one can specify an interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

 for a library
Library (computer science)
In computer science, a library is a collection of resources used to develop software. These may include pre-written code and subroutines, classes, values or type specifications....

.

Class declarations

In C++, function prototypes are also used in class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

definitions.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK