Function prototype

Function prototype

A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function's name, arity, argument types and return type. 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).

Example

As an 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 argument "n" and returns an integer. Elsewhere in the program a function definition must be provided if one wishes to use this function.

Uses

Informing the compiler

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 when the function is applied to some arguments. This can potentially cause problems. The following code illustrates a situation in which the behavior of an implicitly declared function is unspecified.

#include /* * If this prototype is provided, the compiler will catch the error * in main(). If it is omitted, then the error will go unnoticed. */ int fac(int n); /* Prototype */ int main() { /* Calling function */ printf("%d ", 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 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 or the value of a variable that is currently not in scope). 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, one can specify an interface for a library.

Class declarations

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

References

* Citation
last = Kernighan
first = Brian W.
last2 = Ritchie
first2 = Dennis M.
title = The C Programming Language
publisher = Prentice Hall PTR
date = 1988
place = Upper Saddle River, NJ
edition = 2nd
isbn = 0131103628

See also

* Type signature


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Prototype JavaScript Framework — Infobox Software name = Prototype JavaScript Framework developer = [http://prototypejs.org/core Prototype Core Team] latest release version = 1.6.0.3 latest release date = release date|2008|09|28 genre = JavaScript toolkit license = MIT License… …   Wikipedia

  • Prototype — A prototype is an original type, form, or instance of something serving as a typical example, basis, or standard for other things of the same category. The word derives from the Greek πρωτότυπον ( prototypon ), archetype, original , neutral of… …   Wikipedia

  • Prototype (disambiguation) — A prototype is something that is representative of a category of things. Prototype may also refer to:;Automobiles* Citroën Prototype C, range of vehicles created by Citroën from 1955 to 1956 * Citroën Prototype Y, project of replacement of the… …   Wikipedia

  • Prototype — Saltar a navegación, búsqueda Prototype es un framework escrito en JavaScript que se orienta al desarrollo sencillo y dinámico de aplicaciones web. Es una herramienta que implementa las técnicas AJAX y su potencial es aprovechado al máximo cuando …   Wikipedia Español

  • Prototype filter — Prototype filters are electronic filter designs that are used as a template to produce a modified filter design for a particular application. They are an example of a nondimensionalised design from which the desired filter can be scaled or… …   Wikipedia

  • Prototype — У этого термина существуют и другие значения, см. Прототип. Prototype JavaScript Framework Тип JavaScript библиотека Разработчик …   Википедия

  • Prototype (Framework) — Entwickler: Sam Stephenson Aktuelle Version: 1.6.0.3 (29. September 2008) Betriebssystem: unabhängig Kategorie: JavaScript …   Deutsch Wikipedia

  • Prototype (Klassenbibliothek) — Prototype Entwickler Sam Stephenson Aktuelle Version 1.7 (16. November 2010) Betriebssystem plattformunabhängig Programmier­sprache JavaScript …   Deutsch Wikipedia

  • Prototype (фреймворк) — У этого термина существуют и другие значения, см. Прототип. Prototype JavaScript Framework Тип JavaScript библиотека Разработчик Prototype Core Team Операционная система …   Википедия

  • Prototype pattern — The prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to: avoid subclasses of an …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”