Click here to Skip to main content
Click here to Skip to main content

Function prototypes

, 28 Feb 2003
Rate this:
Please Sign up or sign in to vote.
One of the most important features of C++ :: The compiler uses function prototypes to validate function calls...
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Add the rest of your HTML here -->

Introduction

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

------------------------------


// Finding the maximum of three integers
  
#include <iostream.h>
  
   int maximum( int, int, int );   // function prototype
  
   int main()
   {
      int a, b, c;
 
     cout << "Enter three integers: ";
     cin >> a >> b >> c;
 
     // a, b and c below are arguments to
     // the maximum function call
     cout << "Maximum is: " << maximum( a, b, c ) << endl;
 
     return 0;
  }
 
  // Function maximum definition
  // x, y and z below are parameters to
  // the maximum function definition
  int maximum( int x, int y, int z )
  {
     int max = x;
 
     if ( y > max )
        max = y;
 
     if ( z > max )
        max = z;
 
     return max;
  }

  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
double
float
unsigned long int    (synonymous with unsigned long)
long int             (synonymous with long)
unsigned int         (synonymous with unsigned)
int
unsigned short int   (synonymous with unsignd short)
short int            (synonymous with short)
unsigned Char
short
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

#include <iostream.h>
  
   int square( int );   // function prototype
  
   int main()
   {
      for ( int x = 1; x <= 10; x++ )
        cout << square( x ) << "  ";
 
     cout << endl;
     return 0;
  }
 
  // Function definition
  int square( int y )
  {
     return y * y;
  }

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

         

License

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

Share

About the Author

Obanos
Web Developer
United States United States
hey ppl ; im Obanos :: Networks Administration student and MCSA ...Web design ...C++ ...Networking ...
 
;//
--Obanos--

Comments and Discussions

 
GeneralHELP PinsussAnonymous15-Apr-04 10:21 
GeneralNot standard C++ PinmemberChristian Graus3-Mar-03 9:55 
QuestionFunctions in C++? Pinmemberdog_spawn2-Mar-03 10:34 
AnswerRe: Functions in C++? PinmemberChris Losinger2-Mar-03 11:59 
GeneralRe: Functions in C++? Pinmemberdog_spawn2-Mar-03 12:38 
GeneralRe: Functions in C++? PinmemberKevinHall2-Mar-03 17:08 
GeneralRe: Functions in C++? Pinmemberjhwurmbach2-Mar-03 22:08 
GeneralRe: Functions in C++? Pinmemberdog_spawn3-Mar-03 0:01 
GeneralRe: Functions in C++? PinmemberKevinHall3-Mar-03 5:15 
GeneralRe: Functions in C++? PinmemberJohn M. Drescher3-Mar-03 5:34 
GeneralRe: Functions in C++? PinmemberAdamah21-Mar-10 15:18 
AnswerRe: Functions in C++? Pinmemberthenickname17-Dec-05 11:55 
GeneralParameter names... PinmemberMaximilien1-Mar-03 14:20 
GeneralRe: Parameter names... Pinmemberdog_spawn2-Mar-03 10:32 
GeneralRe: Parameter names... PinmemberJörgen Sigvardsson3-Mar-03 7:08 
GeneralRe: Parameter names... PinmemberToUpdate16-Sep-03 19:24 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 1 Mar 2003
Article Copyright 2003 by Obanos
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid