Ask a Question

what is function prototype explain ?

on 2011-03-28 20:07:44   by Aman   on Computer Science & Engineering  2 answers

sanchayita

on 2011-03-29 09:30:00  

A function prototype in C, Perl 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). 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. 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();

ravi

on 2011-03-31 09:30:00  

A function prototype in C, Perl 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 Consider the following function prototype: 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. 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. 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 cause problems. The following code illustrates a situation in which the behavior of an implicitly declared function is undefined.The function \"fac\" expects an integer argument to be on the stack or in a register 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.