Share on Facebook Share on Twitter Email
Answers.com

Function prototype

 
Computer Desktop Encyclopedia: function prototype

In programming, a declaration of a function to the compiler indicating what types of parameters are passed to it and what value is returned. The compiler can then report an error if a function within the program is not written to conform to the prototype. See function.

The following C example is the simplest prototype. The function displays "Hello World" on screen without receiving any parameters or returning any values to the calling instruction. "Void" means "nothing."

         Prototype
         void printHelloWorld(void);


         Function
         void printHelloWorld(void)
         {
         printf ("Hello World\n");
         }

Prototypes can be much more complicated as in this C example where five parameters are passed to a graphics function.

  void   DrawTransparentBitmap(HDC hdc,
                HBITMAP hBitmap,
                int xStart,
                int yStart,
                COLORREF cTransparentColor);

Download Computer Desktop Encyclopedia to your iPhone/iTouch

Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
Wikipedia: Function prototype
Top

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).

Contents

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

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 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 <stdio.h>
 
 /* 
  * 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(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 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

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

See also


 
 

 

Copyrights:

Computer Desktop Encyclopedia. THIS DEFINITION IS FOR PERSONAL USE ONLY.
All other reproduction is strictly prohibited without permission from the publisher.
© 1981-2010 The Computer Language Company Inc.  All rights reserved.  Read more
Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Function prototype" Read more