C++ Function Definition vs Declaration

In C++, understanding the difference between a function declaration and a function definition is crucial for writing efficient and organized code. This tutorial explains both concepts with examples and comparisons.


What is a Function Declaration?

A function declaration, also known as a function prototype, provides the compiler with information about the function’s name, return type, and parameters, but does not include the implementation.

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

In this example, add is a function that takes two integer parameters and returns an integer. The implementation is not provided in the declaration.


What is a Function Definition?

A function definition provides the actual implementation of the function, specifying the operations to be performed when the function is called.

</>
Copy
int add(int a, int b) {
    return a + b;
}

Here, the add function is implemented to return the sum of its two integer parameters.


Key Differences Between Function Declaration and Definition

AspectFunction DeclarationFunction Definition
PurposeIntroduces the function to the compiler.Provides the actual implementation of the function.
SyntaxEnds with a semicolon (;).Enclosed within curly braces ({}).
LocationTypically placed in a header file or before the main() function.Can be placed in the same file as the declaration or in a separate source file.

Examples Combining Declaration and Definition

Example 1: Declaration and Definition in the Same File

In this example, we will declare and define a function add() in the same file main.cpp.

Program – main.cpp

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

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

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

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

Output

Sum: 30
Function declaration
</>
Copy
int add(int a, int b);
Introduces the add function to the compiler, specifying its return type (int), name (add), and the types of its two parameters (int a and int b). The actual implementation of the function is provided later in the program.
int main()
The main function is the entry point of the program. It contains the following steps:
  1. Calls the add function with the arguments 10 and 20.
  2. Receives the result of the function call and prints the sum to the console using cout.
  3. Returns 0 to indicate successful execution.
</>
Copy
cout << "Sum: " << add(10, 20) << endl;
This statement outputs the result of the add function call, which is 30, along with the text "Sum: ".
Function definition
The function definition provides the implementation of the add function:
</>
Copy
int add(int a, int b) {
    return a + b;
}
  1. The function takes two integer parameters, a and b.
  2. It computes the sum of a and b using the + operator.
  3. Returns the computed sum to the calling function.

Example 2: Declaration in a Header File and Definition in a Source File

In this example, we will learn how to declare a function in header file myFunctions.h, then define the function in a library file myFunctions.cpp (like a collection of functions), and then call this function in our main.cpp file.

Header file: myFunctions.h

</>
Copy
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H

int multiply(int a, int b);

#endif

Source file: myFunctions.cpp

</>
Copy
#include "myFunctions.h"

int multiply(int a, int b) {
    return a * b;
}

Main file: main.cpp

</>
Copy
#include <iostream>
#include "myFunctions.h"
using namespace std;

int main() {
    cout << "Product: " << multiply(5, 4) << endl;
    return 0;
}

Explanation

  1. The header file myFunctions.h declares the function multiply. It acts as an interface for other files to use the function without exposing its implementation.
  2. int multiply(int a, int b); is the function declaration, specifying that multiply takes two integer parameters and returns an integer.
  3. The source file myFunctions.cpp implements the function multiply. This file contains the function definition.
  4. #include "myFunctions.h" includes the header file to access the function declaration and ensure consistency between the declaration and definition.
  5. The function multiply is defined as int multiply(int a, int b) { return a * b; }, which multiplies the two input parameters and returns the result.
  6. The main file main.cpp includes both the iostream library and the header file myFunctions.h.
  7. cout << "Product: " << multiply(5, 4) << endl; calls the multiply function, passing 5 and 4 as arguments, and prints the result (20) to the console.
  8. The main file serves as the entry point for the program and links with the source file myFunctions.cpp during compilation to access the function implementation.