Static Functions

Function Declaration vs. Definition: What's the Difference?

A declaration tells the compiler a function's name and signature, while a definition adds the body. See how static changes linkage, with C++ examples.

On this page
  1. What a declaration and a definition each do
  2. Why declarations matter
  3. The one definition rule
  4. Where static fits
  5. Declarations and definitions must match
  6. Key points

What a declaration and a definition each do

A function declaration tells the compiler that a function exists. It gives the name, the return type, and the parameter types, and it ends with a semicolon. A function definition gives the same information and adds the body, the code that runs when the function is called.

int add(int a, int b);   // declaration: no body, ends with a semicolon

int add(int a, int b) {  // definition: same signature, with a body
    return a + b;
}

Parameter names are optional in a declaration, so int add(int, int); is also a valid declaration.

Why declarations matter

The compiler checks each call against a declaration it has already seen. If a call comes before any declaration, the compiler reports an error, usually along the lines of “was not declared in this scope.”

That is why many programs put declarations in a header file and definitions in a source file:

// math_utils.h
int add(int a, int b);
// math_utils.cpp
#include "math_utils.h"

int add(int a, int b) {
    return a + b;
}
// main.cpp
#include <iostream>
#include "math_utils.h"

int main() {
    std::cout << add(2, 3) << '\n';  // prints 5
}

Build it with g++ -std=c++17 main.cpp math_utils.cpp -o demo. Real projects often protect a header with a header guard in C++, so that a second include does not repeat the declarations.

The one definition rule

A function can be declared many times. A normal (non-inline) function that the program uses must have exactly one definition across the whole program. If two source files each define add in this way, the linker usually reports a “multiple definition” error.

The exception is a function marked inline, which may be defined in several files as long as every definition is the same. The C++ inline keyword covers that case.

Where static fits

static does not decide whether a function is declared or defined. It changes linkage: a static function’s name is visible only inside its own file. So two files can each define a static function with the same name, and the linker keeps them apart:

// file_a.cpp
static int clamp_percent(int value) {
    if (value < 0) return 0;
    if (value > 100) return 100;
    return value;
}

int battery_level(int raw) {
    return clamp_percent(raw);
}
// file_b.cpp
static int clamp_percent(int value) {  // a different function, not a duplicate
    return value < 0 ? 0 : value;
}

int volume_level(int raw) {
    return clamp_percent(raw);
}

Keep the keyword consistent. If a function is declared without static and later declared with it, the compiler reports an error. For the broader picture of functions that only one file can see, see static functions in C++.

Declarations and definitions must match

The declaration and the definition must agree on the signature. If the parameter types differ, the definition is a different function, an overload, and not a second definition of the one you declared. A call that expects the declared version then has nothing to link to, and the linker reports an undefined reference.

Key points

  • A declaration gives the name, return type, and parameter types, and it can appear more than once.
  • A definition adds the body. A normal function that is used needs exactly one definition in the program.
  • static on a function limits its name to the file where it is defined.

The complete guide to static functions

Static Functions in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The sizeof Operator in C++

Learn how the sizeof operator in C++ works: what it returns in bytes, how it handles arrays, pointers and structs, and common mistakes to avoid.

sizeof 6 min read

Bitwise XOR in C++

Learn how the bitwise XOR operator (^) in C++ works, with a truth table, code examples, and common uses such as toggling bits and swapping values.