CPP Function Prototyping

What is Functions Prototyping.

  • The C++ compiler requires knowing the function before any call can be made. As in the Example above the function DemonstrationFunction() is known by the compiler when the call to the function, DemonstrationFunction(), is made inside the main function.
  • Many programmers want to organize their functions into files so the functions can be used by all other application files.
  • As the C++ compiler requires knowing the function, you must prototype the function you want to use if it is not in the file where the call to the function is done.
    Function prototyping:
    Function prototyping:
    //                   Method    Type of each
    // return type       name      parameter
     unsigned short int getArea  ( int width, int height);
    // Prototyped function always end with semicolon.
    // Prototyped function is also called function declaration.
    // Parameter names are not required in the prototyping.
    Program example of function prototyping:
    Source code Result
    #include <iostream>
    // Prototyping of the function
    double Convert(double);
    // or
    // double Convert(double tFahrenheit);
    int main()
      using namespace std;
      double tFahrenheit;
      double tCelsius;
      cout << "Temperature in Fahrenheit: ";
      cin >> tFahrenheit;
      // call to the function
      tCelsius = Convert(tFahrenheit);
      cout << "Temperature in Celsius is: ";
      cout << tCelsius << endl;
      return 0;
    // Implementation of the function  
    double Convert(double tFahrenheit) {
      return ((tFahrenheit - 32) * 5) / 9;
    Temperature in Fahrenheit: 68
    Temperature in Celsius is: 20

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.