100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Function Declaration and Definition in C++

Understand the difference between a function declaration (prototype) and a function definition in C++, and when each is required.

FunctionsBeginner6 min readJul 7, 2026
Analogies

1. Intro

In C++, a function's declaration and its definition are two distinct concepts. A declaration (also called a prototype) tells the compiler the function's name, return type, and parameter types so the function can be called before its full body appears later in the file or in another file. A definition actually supplies the function body — the code that runs when the function is called.

🏏

Cricket analogy: A team sheet naming a bowler and their role is like a declaration announcing they exist and what they'll do, while their actual overs bowled in the match are the definition — the real performance.

2. Syntax

cpp
// Declaration (prototype) - ends with a semicolon, no body
int add(int a, int b);

// Definition - includes the body
int add(int a, int b) {
    return a + b;
}

3. Explanation

A declaration is essentially a promise to the compiler: "this function exists somewhere with this exact signature." This lets you call the function from main() even if its definition appears later in the file, because the compiler already knows how to check the call for correctness. A definition, by contrast, includes the { ... } body and is what actually gets compiled into executable instructions. A function can be declared multiple times (in multiple files via header files) but must be defined exactly once — this is known as the One Definition Rule (ODR). If you only define a function above where it is used, a separate declaration is not required, since the definition itself also acts as a declaration.

🏏

Cricket analogy: Announcing 'our No. 4 batsman will play' before the toss lets the scorer track the lineup even before he walks in, just as a declaration lets the compiler check calls before seeing the function body.

4. Example

cpp
#include <iostream>
using namespace std;

// Declaration (prototype) placed before main
int multiply(int a, int b);

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

// Definition placed after main
int multiply(int a, int b) {
    return a * b;
}

5. Output

text
Product: 24

6. Key Takeaways

  • A declaration (prototype) tells the compiler the function's signature without providing a body.
  • A definition provides the actual body/implementation of the function.
  • Declarations let you call a function before its definition appears in the code.
  • A function can be declared many times but must be defined only once (One Definition Rule).

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#FunctionDeclarationAndDefinitionInC#Function#Declaration#Definition#Syntax#Functions#StudyNotes#SkillVeris