C++ Function Syntax: A Complete Guide

Formulas & functions · Updated November 22, 2026

The basic syntax for a function in C++ is return_type function_name(parameter_list) { body }. For example, int add(int a, int b) { return a + b; } declares a function named add that takes two integers and returns their sum. This article explains every part of that syntax, how to use it correctly, and common mistakes to avoid.

Anatomy of a Function Declaration

Every C++ function has a signature that tells the compiler what it returns, what it takes, and how to call it. The parts are:

  • Return type – the data type of the value the function sends back (e.g., int, double, void for no return).
  • Function name – an identifier that follows C++ naming rules (letters, digits, underscores; cannot start with a digit).
  • Parameter list – a comma-separated list of parameters, each with a type and a name. Empty parentheses mean no parameters.
  • Function body – a block of statements enclosed in curly braces { } that defines what the function does.

Here is a complete example:

double area(double radius) {
    const double pi = 3.14159;
    return pi * radius * radius;
}

This function returns a double, takes one double parameter, and uses a local constant. The return statement sends the computed value back to the caller.

Function Declarations vs. Definitions

In C++, you can separate the declaration (prototype) from the definition. A declaration tells the compiler the function exists and its signature, without the body. A definition includes the body. This separation is common in header files.

Declaration:

int multiply(int a, int b);

Definition:

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

If you call a function before its definition and there is no prior declaration, the compiler will error. Placing declarations in headers allows multiple source files to use the same function.

Parameters: Pass by Value, Reference, and Const Reference

By default, C++ passes parameters by value, meaning the function gets a copy. Modifying the copy does not affect the original. To modify the original or avoid copying large objects, use references.

  • Pass by value – void func(int x) – copies the value; safe for small types.
  • Pass by reference – void func(int& x) – allows modification of the original variable.
  • Pass by const reference – void func(const std::string& s) – read-only access, avoids copying.

For example:

void increment(int& value) {
    ++value;
}

int main() {
    int n = 5;
    increment(n);
    // n is now 6
}

Using const reference for large objects like std::string or std::vector is a good practice to avoid unnecessary copies.

Return Values and void

A function that returns a value must have a return statement with a matching type. A function that returns nothing is declared with void. You can use return; to exit early from a void function.

void printMessage(const std::string& msg) {
    std::cout << msg << std::endl;
    return; // optional
}

For non-void functions, every code path must return a value. The compiler will warn or error if you miss a return in some branches.

Default Arguments and Function Overloading

C++ allows default values for parameters, which must be specified from right to left. For example:

void greet(const std::string& name, const std::string& greeting = "Hello") {
    std::cout << greeting << ", " << name << "!" << std::endl;
}

You can call greet("Alice") or greet("Bob", "Hi"). Function overloading lets you define multiple functions with the same name but different parameter lists. The compiler picks the right one based on arguments.

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

Overloading cannot differ only by return type; the parameter lists must differ.

Common Mistakes and Pitfalls

Several syntax errors are frequent for beginners:

  • Missing semicolon after a function declaration (but not after a definition).
  • Mismatched braces – every { must have a matching }.
  • Wrong return type – returning a value from a void function or forgetting to return from a non-void function.
  • Incorrect parameter types – passing a double to an int parameter causes truncation.
  • Calling a function before declaration – the compiler needs to see the prototype first.

A common misconception is that arrays are passed by value. In C++, when you pass an array, it decays to a pointer, so modifications affect the original. To pass an array by value, you need to wrap it in a struct or use std::array.

Function Pointers and Lambdas (Briefly)

Functions in C++ can be stored in pointers and passed as arguments. The syntax for a function pointer is return_type (*pointer_name)(parameter_types). For example:

int (*op)(int, int) = &add;
int result = op(3, 4); // calls add(3, 4)

Modern C++ also offers lambdas, which are anonymous function objects. They have a concise syntax: [capture](parameters) -> return_type { body }. For instance:

auto multiply = [](int a, int b) { return a * b; };

Lambdas are useful for short, local functions, especially with algorithms like std::sort.

Frequently Asked Questions

What is the difference between a function declaration and a definition?

A declaration (prototype) only gives the signature, ending with a semicolon. A definition includes the body. You can have many declarations but only one definition in a program (unless it’s an inline function).

Can a function return multiple values?

Not directly, but you can return a std::pair, std::tuple, a struct, or use output parameters (references). For example, std::pair<int, int> minMax(int a, int b) returns two values.

What does void mean in a function?

void as a return type means the function returns nothing. As a parameter list, void func(void) is equivalent to void func() and means no parameters (in C, but in C++ both are the same).

Why do we need function prototypes?

Prototypes allow the compiler to check that you call a function with the correct arguments and return type, and they enable calling a function before its definition appears in the source file. They are essential for multi-file programs.

What is the best way to pass a string to a function?

Pass it by const reference: void func(const std::string& s). This avoids copying the string and prevents accidental modification. Only pass by value if you need a local copy to modify.

Can I have a function with no name?

Yes, lambdas are unnamed functions. They are defined inline and can be stored in variables or passed directly to algorithms.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.