Open In App

Function Prototypes in C++

Last Updated : 16 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, function prototype is a function declaration that tells the compiler about the name of the function, its return type and the number and type of parameters. With this information, the compiler ensures that function calls can be linked to the function definition, even if the definition is not available at the time of call.

Syntax

The general syntax of the function prototype is as follows:

C++
return_type name(type1, type2 ....);

where,

  • return_type : Type of value the function returns.
  • name : Name of the function.
  • type1, type2 .... : Type of the parameters. Specifying their names is optional.

A function prototype can be used at any place instead of function declaration such as when a function is called before it is defined.

Examples

C++
// Not a prototype as it does not contain the
// number and type of parameters. It is only a
// declaration
int multiply();

// Valid prototype as it contains function name,
// return type, number, and type of parameters
int multiply(int, int);

// Also a valid prototype
int multiply(int x, int y);

// Function definition inherently contains
// function prototype
int multiply(int x, int y) {
    return x * y;
}

What Happens If the Function Prototype Is Missing?

In C++, it is not compulsory to provide function prototype in all cases. It is only necessary when the function is defined after its call. The compiler will generate an error or warning depending on the situation.

Consider the following program:

C++
#include <iostream>
using namespace std;

int main() {
    // Trying to use a function before declaring
    // or defining it
    cout << square(5) << endl;
    return 0;
}

// Function defined after main
int square(int n) {
    return n * n;
}


Output

./Solution.cpp: In function 'int main()':
./Solution.cpp:7:21: error: 'square' was not declared in this scope
cout << square(5) << endl;

Explanation: In the above code, we tried to call the square() function before it was declared or defined. Since C++ requires functions to be declared or defined before they are used, this leads to a compilation error. Unlike C, C++ does not assume a default return type of int when the prototype is missing.

Adding the prototype at the top fixes the issue:

C++
#include <iostream>
using namespace std;

// Function prototype
int square(int);

int main() {
    cout << square(5) << endl;
    return 0;
}

int square(int n) {
    return n * n;
}

Output
25

Benefits of Function Prototypes

Here are the key benefits of including function prototypes in your C++ program:

  • Function Declaration Before Definition: A function can be called before its definition. This is common in large programs where prototypes are placed in header files or at the top of the source file.
  • Type Checking: The compiler ensures that the function is called with the correct type and number of arguments. Any mismatch is reported at compile time.
  • Code Clarity: Prototypes act as documentation for the function’s purpose and expected parameters, making the code easier to read and maintain.

Function Declaration vs Function Prototype

Though the terms "function declaration" and "function prototype" are often used interchangeably, there are subtle differences:

Function DeclarationFunction Prototype
Tells the compiler about the existence of a function.Tells the compiler about the existence and signature of the function.
Can include only the function’s name and return type.Must include function’s name, return type, and parameter types.
Typically used in header files to declare functions.Used to ensure type checking for function calls before definition.

Example

C++
// Function Declaration
return_type function_name();
// Function Prototype
return_type function_name(parameter_list);

Next Article
Article Tags :
Practice Tags :

Similar Reads