Click here to Skip to main content
14,736,835 members
Articles » Languages » C / C++ Language » General
Posted 28 Feb 2003


14 bookmarked

Function prototypes

Rate me:
Please Sign up or sign in to vote.
1.09/5 (24 votes)
28 Feb 2003
One of the most important features of C++ :: The compiler uses function prototypes to validate function calls...


 Function Prototypes

One of the most important features of C++ is the function prototypes. A function prototype tells the compiler the name of the function, the type of data returned by the function, the number of parameters the function expects to receive, the types of the parameters, and the order in which these parameters are expected. The compiler use function prototypes to validate function calls. Early versions of C did not perform this kind of checking, so it was possible to call functions improperly without the compiler detecting the errors Such calls could result in fatal execution-time errors or nonfatal fatal errors that caused, difficult to detect logic errors. Function prototypes correct this deficiency.

The function prototype for maximum in this program is

int maximum( int, int, int);


<P><BR>// Finding the maximum of three integers<BR>   <BR>#include <iostream.h><BR>   <BR>   int maximum( int, int, int );   // function prototype<BR>   <BR>   int main()<BR>   {<BR>      int a, b, c;<BR>  <BR>     cout << "Enter three integers: ";<BR>     cin >> a >> b >> c;<BR>  <BR>     // a, b and c below are arguments to <BR>     // the maximum function call<BR>     cout << "Maximum is: " << maximum( a, b, c ) << endl;<BR>  <BR>     return 0;<BR>  }<BR>  <BR>  // Function maximum definition<BR>  // x, y and z below are parameters to <BR>  // the maximum function definition<BR>  int maximum( int x, int y, int z )<BR>  {<BR>     int max = x;<BR>  <BR>     if ( y > max )<BR>        max = y;<BR>  <BR>     if ( z > max )<BR>        max = z;<BR>  <BR>     return max;<BR>  }</P>
  This prototype states that maximum takes three arguments of type int, and returns a result of type int. Notice that this function prototype is the same as the header of the function definition of maximum except the names of the parameters(x, y,and z) are not included.

The portion of a function prototype that includes the name of the function and the types of its argumnets is called the function signture or simply the signture. The function signture dose not include the return type of the function.

// A function call that dose not match the function prototype is a syntax error.

As an example of the preceding Common Programming error, in the previous program, if the function prototype had been written

  void maximum( int, int, int);

the compiler would report an error because the void return type in the function prototype would differ from the int return type in the function header.

Another important feature of function prototypes is the coercion of arguments, i.e, the forcing of arguments to the appropriate type. For example, the math library function (sqrt) can be called with an integer argument even though the function prototype in math.h speifies a double argument, and the function will still work correctly. The statement

cout << sqrt( 4 );

correctly evaluates sqrt(4), and prints the value 2. The function prototype causes the compiler to convert the integer argument value 4 to the double value 4.0 before the value is passed to sqrt. In general, argumnet values that do not correspond percisely to the parameter types in the function prototype are converted to the proper type before the function is called. These conversions can lead to incorrect results if C++'s promotion rules are not followed.

the Promotion rules specify how types can be converted to other types without losing data. In our sqrt example above, an int is automatically converted to a double without changing its value. However, a double value converted to an int truncates the fractional part of the double value. Converting large integer to small integer types (e.g, long to short) may also result in changed values.

The promotion rules apply to expressions containing values of two or more data types; such expressions are also referred to as mixed-type expressions. The type of each value in a mixed-type expression is promoted to the "highest" type in the expression. Another common use of promotion is when the type of an argument to a function dose not match the parameter type specified in the function definition. Here is a list of the built-in data types in order from "highest type" to "lowest type" :
Promotion hierarchy for built-in data types..

long double
unsigned long int    (synonymous with unsigned long)
long int             (synonymous with long)
unsigned int         (synonymous with unsigned)
unsigned short int   (synonymous with unsignd short)
short int            (synonymous with short)
unsigned Char

Converting values to lower types can result in incorrect values. Therefore, a value can only be converted to a lower type

*/ Converting from a higher data type in the promotion hierarchy to a lower type can change the data value.*/

by explicitly assigning the value to a variable of lower type, or by using a cast operator. Function argument values are converted to the parameter types in a function prototype as if they are being assigned directly to variables of those types. If our square function that uses an integer parameter

<P>#include <iostream.h><BR>   <BR>   int square( int );   // function prototype<BR>   <BR>   int main()<BR>   {<BR>      for ( int x = 1; x <= 10; x++ )<BR>        cout << square( x ) << "  ";<BR>  <BR>     cout << endl;<BR>     return 0;<BR>  }<BR>  <BR>  // Function definition <BR>  int square( int y )<BR>  {<BR>     return y * y;<BR>  }</P>
is called with a floating-point argument, the argument is converted to int (lower type), and square usually returns an incorrect value. For example, square(4.5) would return 16 not 20.25



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

GeneralHELP Pin
Anonymous15-Apr-04 11:21
MemberAnonymous15-Apr-04 11:21 
GeneralNot standard C++ Pin
Christian Graus3-Mar-03 10:55
protectorChristian Graus3-Mar-03 10:55 
QuestionFunctions in C++? Pin
dog_spawn2-Mar-03 11:34
Memberdog_spawn2-Mar-03 11:34 
AnswerRe: Functions in C++? Pin
Chris Losinger2-Mar-03 12:59
professionalChris Losinger2-Mar-03 12:59 
GeneralRe: Functions in C++? Pin
dog_spawn2-Mar-03 13:38
Memberdog_spawn2-Mar-03 13:38 
GeneralRe: Functions in C++? Pin
KevinHall2-Mar-03 18:08
MemberKevinHall2-Mar-03 18:08 
GeneralRe: Functions in C++? Pin
jhwurmbach2-Mar-03 23:08
Memberjhwurmbach2-Mar-03 23:08 
GeneralRe: Functions in C++? Pin
dog_spawn3-Mar-03 1:01
Memberdog_spawn3-Mar-03 1:01 
GeneralRe: Functions in C++? Pin
KevinHall3-Mar-03 6:15
MemberKevinHall3-Mar-03 6:15 
GeneralRe: Functions in C++? Pin
John M. Drescher3-Mar-03 6:34
MemberJohn M. Drescher3-Mar-03 6:34 
GeneralRe: Functions in C++? Pin
Adamah21-Mar-10 16:18
MemberAdamah21-Mar-10 16:18 
AnswerRe: Functions in C++? Pin
thenickname17-Dec-05 12:55
Memberthenickname17-Dec-05 12:55 
GeneralParameter names... Pin
Maximilien1-Mar-03 15:20
MemberMaximilien1-Mar-03 15:20 
GeneralRe: Parameter names... Pin
dog_spawn2-Mar-03 11:32
Memberdog_spawn2-Mar-03 11:32 
GeneralRe: Parameter names... Pin
Jörgen Sigvardsson3-Mar-03 8:08
MemberJörgen Sigvardsson3-Mar-03 8:08 
GeneralRe: Parameter names... Pin
ToUpdate16-Sep-03 20:24
MemberToUpdate16-Sep-03 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.