C++

C++ Functions: Parameters, Overloads, and Default Arguments

A function is a named block of code that takes parameters and can return a value. This guide runs one main program. The program defines two functions with the same name, add, and the compiler picks one by the argument types: add int=5 and add double=6. It defines a function with a default argument, so mult(5) prints 10 and mult(5, 3) prints 15. It swaps two variables through reference parameters and prints swapped x=2 y=1. Three smaller programs then show the traps: a value parameter that changes nothing in the caller, a mixed call the compiler refuses as ambiguous, and a function that returns a reference to its own local variable.

Written November 11, 2026 · 4 min read

By the end of this guide
  • The main program: overloads, a default, and a swap
  • Declaration, definition, and the call
  • Pass by value: the function gets a copy
  • Overloads: one name, different parameter lists

What this guide is

A function packages a job under a name. The caller passes arguments, the function runs its body with those values in its parameters, and a return value can come back. C++ adds three rules on top of that shape. A parameter can be a copy of the argument or a reference to it, and the choice decides whether the function can change the caller's variable. Two functions can share one name when their parameter lists differ, and the compiler picks between them at each call. A trailing parameter can carry a default value, so a caller may leave that argument out. This guide runs one main program that uses all three rules, plus three smaller programs that isolate the traps.

The main program: overloads, a default, and a swap

C++
#include <iostream>
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int mult(int a, int b = 2) { return a * b; }
void swap_ref(int& a, int& b) { int t = a; a = b; b = t; }
int main() {
    std::cout << "add int=" << add(2, 3) << "\n";
    std::cout << "add double=" << add(2.5, 3.5) << "\n";
    std::cout << "mult default=" << mult(5) << " mult both=" << mult(5, 3) << "\n";
    int x = 1, y = 2; swap_ref(x, y);
    std::cout << "swapped x=" << x << " y=" << y << "\n";
    return 0;
}

The exact output:

Output
add int=5
add double=6
mult default=10 mult both=15
swapped x=2 y=1

Each function in the file shows one idea. The two add functions share a name and differ only in their parameter types; they are overloads. The mult function gives its second parameter a default value of 2, so the call with one argument still has both values to multiply. The swap_ref function returns nothing, marked by the void return type, and works through its reference parameters instead. The sections below take each idea in turn.

Declaration, definition, and the call

A function definition gives the return type, the name, the parameter list, and the body. The line int add(int a, int b) { return a + b; } says the function returns an int, is named add, and takes two int parameters named a and b. A call such as add(2, 3) binds the argument 2 to a and the argument 3 to b, runs the body, and hands the returned 5 back to the caller. The caller in the main program prints that value, which is why the first line reads add int=5.

A declaration, also called a prototype, gives the same first line without the body and ends with a semicolon: int add(int a, int b);. A declaration tells the compiler the name, the return type, and the parameter types, so code can call the function before its definition appears in the file. The definition itself still has to exist once in the program. Small programs such as the ones in this guide define each function above main, so no separate declaration is needed. Larger programs split across files put declarations in a header and definitions in one source file each.

Pass by value: the function gets a copy

A plain parameter is a copy. The function receives its own variable holding the argument's value, and changes to that variable stay inside the function. A second program makes the point with one variable each way:

C++
#include <iostream>
void bump_value(int n) { n += 10; }
void bump_ref(int& n) { n += 10; }
int main() {
    int a = 5; bump_value(a);
    std::cout << "after value a=" << a << "\n";
    int b = 5; bump_ref(b);
    std::cout << "after ref b=" << b << "\n";
    return 0;
}

The exact output:

Output
after value a=5
after ref b=15

The function bump_value adds 10 to its copy of the argument, so the caller's a still holds 5 when the call returns. The function bump_ref declares its parameter as int&, a reference, so its parameter is the caller's own variable under another name; adding 10 changes b itself to 15. Pass by value is the safe default for small values the function only reads, such as the two numbers in add. A reference parameter fits when the function must write back into the caller's variable, as a swap must, or when the value is large and copying it would waste time. A reference the function only reads is usually written const plus the reference mark, which promises the function will not change it.

Overloads: one name, different parameter lists

Overloading means two or more functions share a name and differ in their parameter lists, by type or by count. The main program has two: add(int, int) and add(double, double). At each call the compiler compares the argument types with each candidate's parameter types and picks the function that matches, a step called overload resolution. The call add(2, 3) passes two whole numbers, so it runs the int version and returns 5. The call add(2.5, 3.5) passes two decimal numbers, so it runs the double version and returns 6, printed without a decimal part because the value is whole.

The return type alone cannot separate two overloads. Two functions that differ only in what they return have the same parameter list, and the compiler has no way to choose between them at a call, so that pair does not compile. The name and the parameter list together form the function's signature, and the signature is what must differ.

Trap: a mixed call can be ambiguous

Trap
Overload resolution needs one best match. A call that fits two overloads equally well has none, and the compiler refuses it. A third program calls the same two add functions with one whole number and one decimal number:
C++
#include <iostream>
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int main() { std::cout << add(2, 3.5) << "\n"; return 0; }

This program does not compile. The compiler's error, verbatim:

Output
error: call of overloaded 'add(int, double)' is ambiguous
note: candidate: 'int add(int, int)'
note: candidate: 'double add(double, double)'

Each candidate needs one conversion. The int version must convert 3.5 to an int, and the double version must convert 2 to a double. Neither conversion set is better than the other, so there is no best match and the build fails with exit code 1. The fix sits at the call site: make both arguments the same kind, as in add(2.0, 3.5) or add(2, 3), or convert one argument on purpose so the intended overload wins. An ambiguous call is a compile error, never a silent pick, which is the safe outcome: the program states which function it means instead of guessing.

Key result
add(2, 3.5) fails to compile with "call of overloaded 'add(int, double)' is ambiguous", naming both candidates. Each overload needs one conversion, so neither wins; matching the argument types at the call fixes it.

Default arguments: a value the caller may skip

A default argument gives a parameter a value to use when the call leaves that argument out. The main program defines int mult(int a, int b = 2). The call mult(5) passes only a, so b takes its default 2 and the result is 10. The call mult(5, 3) passes both, so the default is ignored and the result is 15. Both numbers appear on one output line: mult default=10 mult both=15.

Two rules govern defaults. First, they fill in from the right: once a parameter has a default, every parameter after it must have one too, because a call supplies arguments from the left and can only leave out a trailing run. A definition that breaks this, int f(int a = 1, int b), does not compile; the compiler's error reads "default argument missing for parameter 2", with a note pointing at the parameter that has the default. Second, the default is written once. It normally sits on the first declaration the caller sees, and no later declaration or definition may repeat a default that is already given — but a later declaration, or the definition itself, may add a default for a parameter that does not have one yet, and calls after that point can use it.

Trap: never return a reference to a local

Trap
A reference parameter can write back because it names the caller's variable, which outlives the call. A local variable inside the function does not. Returning a reference to a local hands the caller a name for storage that ends when the function returns, and reading through it is undefined behavior. A fourth program shows the shape:
C++
#include <iostream>
int& bad() { int local = 42; return local; }
int main() { std::cout << bad() << "\n"; return 0; }

This program compiles, but the compiler warns about it during the build. The warning, verbatim:

Output
warning: reference to local variable 'local' returned [-Wreturn-local-addr]

That warning is the whole lesson. The program is not quoted with an output, because any value it printed would be luck, not a result. The safe forms return a value, as add and mult do, or write through a reference parameter, as swap_ref does. Treat this warning as an error in your own code: a function that triggers it has a bug even when the program seems to run.

Key result
returning a reference to a local variable compiles with the warning "reference to local variable 'local' returned". The storage ends at the return, so the caller's reference names nothing valid. Return a value or write through a parameter instead.

Which parameter form for which job

Job Form Why
A small value the function only reads Pass by value, as in add(int, int) The copy is cheap and the caller's variable cannot change, as the a=5 output shows
A value the function must change for the caller Reference, as in swap_ref(int&, int&) The parameter is the caller's variable, as the swapped x=2 y=1 output shows
A large value the function only reads Constant reference No copy is made and the function promises not to change the value
One name for the same job on different types Overloads, as in the two add functions The compiler picks by argument types at each call
A trailing value most calls share Default argument, as in mult(int, int = 2) Callers may leave it out, and the default fills in

The deciding question is who owns the value after the call. A copy belongs to the function. A reference belongs to the caller, which is why it can carry a result back and why it must never name a local.

Checklist

  • Before choosing a parameter form, decide whether the function must change the caller's variable. If not, pass small values by value.
  • Before writing a swap or any two-way update, declare the parameters as references. A value version compiles and silently changes nothing.
  • Before adding an overload, check that its parameter list differs from every same-name function. A different return type alone is not an overload.
  • Before calling an overloaded name with mixed argument types, check that one candidate is a clear best match. If each needs a conversion, the call is ambiguous.
  • Before adding a default argument, check that every parameter after it also has one. Defaults form a trailing run.
  • Before returning a reference, check that the named variable outlives the call. A local variable never does.

Sources

Keep Exploring