CppFunctions

Functions

A function is a named, reusable block of code that performs a specific task. Functions let you break a program into smaller, understandable pieces, avoid repeating the same code, and give behavior a clear, descriptive name.
Declaration vs Definition
A function declaration (also called a prototype) tells the compiler a function's name, return type, and parameter types — without providing its body. A function definition includes the full body, the actual implementation.

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

// Definition - includes the body
int add(int a, int b) {
    return a + b;
}
Note
Header files typically contain only declarations, while the matching .cpp source file contains the definitions. This lets other files include the header and call the function without needing to see (or recompile) its implementation, which is the basis of how C++ separates interface from implementation across multiple files.
Anatomy of a Function

CPP
returnType functionName(parameterType parameterName, ...) {
    // function body
    return value; // only if returnType is not void
}
The return type specifies what kind of value the function sends back to its caller. Every code path through a function with a non-void return type must return a value of that type (or something implicitly convertible to it).
The void Return Type
A function that doesn't need to produce a value uses void as its return type. A void function can still use a bare return; to exit early, but it cannot return a value.

CPP
#include <iostream>
using namespace std;

void greet(string name) {
    cout << "Hello, " << name << "!" << endl;
    // no return statement needed
}

int main() {
    greet("Alice");   // Output: Hello, Alice!
    greet("Bob");     // Output: Hello, Bob!
    return 0;
}
Calling Functions

Calling a function means using its name followed by parentheses containing any required arguments. The function's return value (if any) can be stored, printed, or used directly in an expression.

CPP
#include <iostream>
using namespace std;

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

int main() {
    int result = square(5);       // store the return value
    cout << result << endl;       // Output: 25
    cout << square(3) + 1 << endl; // Output: 10 - used directly in an expression
    return 0;
}
Where to Declare: Before Use, or Forward Declare

C++ compiles top to bottom and needs to know a function's signature before it's called. You can satisfy this in two ways: define the function above where it's first used, or write a forward declaration near the top of the file (or in a header) and define the function anywhere afterward.

CPP
#include <iostream>
using namespace std;

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

int main() {
    cout << add(2, 3) << endl; // works - compiler already knows the signature
    return 0;
}

// definition can come after main()
int add(int a, int b) {
    return a + b;
}
Warning
Without a forward declaration, calling a function before it is defined in the same file results in a compiler error such as 'add' was not declared in this scope. Forward declarations (and header files, which are essentially collections of them) are how larger C++ programs organize functions across files while still following this top-to-bottom rule.
Key Points
  • A declaration gives the compiler a function's signature; a definition provides its body.

  • Header files hold declarations so other files can call a function without seeing its implementation.

  • void functions perform an action but return no value; return; can still exit early.

  • A function must be declared (or defined) before its first call in a file.

  • Every code path in a non-void function must return an appropriate value.