C++ Function Declaration

In this tutorial, you will learn the concept of function declaration in C++, its syntax, usage, and examples.


What is a Function Declaration?

A function declaration, also known as a function prototype, informs the compiler about the name, return type, and parameters of a function.

A function declaration is typically written before the main() function or in a header file to allow reuse in multiple source files.


Syntax of a Function Declaration

</>
Copy
return_type function_name(parameter_list);

Here’s a breakdown of the syntax:

  • return_type: The data type of the value the function returns (e.g., int, void).
  • function_name: The unique identifier for the function.
  • parameter_list: A comma-separated list of parameters, each with a type and name.

Example of Function Declaration

Below is an example of declaring a function:

</>
Copy
int add(int a, int b);

This declaration states that the function add takes two int parameters and returns an int.

1. Function with void Return Type

</>
Copy
void printMessage();

Explanation: The function printMessage takes no parameters and does not return a value.

2. Function with a double Return Type

</>
Copy
double calculateArea(double radius);

Explanation: The function calculateArea takes a single double parameter (radius) and returns a double value representing the area.

3. Function with Multiple Parameters

</>
Copy
float computeAverage(int num1, int num2, int num3);

Explanation: The function computeAverage accepts three int parameters and returns a float value.

4. Function Returning a char

</>
Copy
char getGrade(int score);

Explanation: The function getGrade takes an int parameter (score) and returns a char value representing the grade.

5. Function with Default Parameter Values

</>
Copy
int multiply(int a, int b = 1);

Explanation: The function multiply has two parameters. The second parameter, b, has a default value of 1. If only one argument is passed, b will default to 1.

6. Function with Array Parameter

</>
Copy
void printArray(int arr[], int size);

Explanation: The function printArray takes two parameters: an array of integers arr and its size size. It does not return a value.

7. Function Returning a Pointer

</>
Copy
int* findMax(int* arr, int size);

Explanation: The function findMax takes a pointer to an integer array arr and its size size. It returns a pointer to the largest integer in the array.

8. Function with Constant Parameters

</>
Copy
double calculateDistance(const double x1, const double y1, const double x2, const double y2);

Explanation: The function calculateDistance takes four constant double parameters representing two points in a plane. It returns the distance between them.


Complete Example with Function Definition

The following code demonstrates both the declaration and definition of a function:

Program – main.cpp

</>
Copy
#include <iostream>
using namespace std;

// Function declaration
int add(int a, int b);

int main() {
    int num1 = 10, num2 = 20;
    cout << "Sum: " << add(num1, num2) << endl;
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;
}

Before the main() function, we have made the function declaration for add(). And then we used the add() function inside the main(). We then defined the add() function after the main function.

Detailed Explanation of the Code

Function Declaration
</>
Copy
int add(int a, int b);
This is the function declaration (also called the function prototype). It tells the compiler the function name (add), return type (int), and parameters (int a, int b). The actual implementation of the function comes later in the code.
Main Function
</>
Copy
int main() {
    int num1 = 10, num2 = 20;
    cout << "Sum: " << add(num1, num2) << endl;
    return 0;
}
The main function is the entry point of the program. It performs the following steps:
1. Two integer variables, num1 and num2, are initialized with values 10 and 20.
2. The add function is called with num1 and num2 as arguments.
3. The result of the add function is printed to the console using cout.
4. The program returns 0, indicating successful execution.
Function Definition
</>
Copy
int add(int a, int b) {
    return a + b;
}
The add function is defined here. It performs the actual computation:
1. The function takes two integer parameters, a and b.
2. It computes the sum of a and b using the + operator.
3. The result is returned to the calling function (in this case, main).

Key Points about Function Declaration

  1. The function declaration ends with a semicolon (;).
  2. If a function is declared but not defined, the program will result in a linker error.
  3. Function declarations are often placed in header files to enable modular programming.

Function Declaration vs. Definition

The following table gives the differences between function declaration and function definition with respect to purpose, syntax, and location in the program.

AspectFunction DeclarationFunction Definition
PurposeIntroduces the function to the compiler.Provides the actual implementation of the function.
SyntaxEnds with a semicolon (;).Enclosed within curly braces ({}).
LocationBefore the main() function or in a header file.In the source file.
Function Declaration vs. Definition

Best Practices for Function Declaration

  1. Use meaningful names for functions and parameters for better readability.
  2. Declare functions in header files and define them in source files for cleaner code organization.
  3. Ensure the function declaration matches the function definition exactly in terms of return type and parameters.