TExcelTAD
Home › C++ › C++ Function Parameters

C++ Function Parameters

Parameters are the variables listed in a function's definition; arguments are the actual values you pass when calling it. Parameters turn a fixed piece of code into a flexible tool.

Multiple parameters

Separate parameters with commas. Each one needs its own type, and arguments are matched by position:

cpp · try it yourself
#include <iostream>
#include <string>
using namespace std;

void introduce(string name, int age, string city) {
    cout << name << " is " << age << " and lives in " << city << ".\n";
}

int main() {
    introduce("Mia", 27, "Oslo");
    introduce("Tomas", 34, "Lima");
}
Mia is 27 and lives in Oslo.
Tomas is 34 and lives in Lima.

Default arguments

A parameter can have a default value that is used when the caller leaves it out. Defaults must be at the end of the parameter list:

cpp · try it yourself
#include <iostream>
#include <string>
using namespace std;

void greet(string name, string greeting = "Hello", char end = '!') {
    cout << greeting << ", " << name << end << "\n";
}

double price(double base, double taxRate = 0.2) {
    return base * (1 + taxRate);
}

int main() {
    greet("Sam");
    greet("Sam", "Welcome back");
    greet("Sam", "Good night", '.');

    cout << price(100) << "\n";
    cout << price(100, 0.05) << "\n";
}
Hello, Sam!
Welcome back, Sam!
Good night, Sam.
120
105

Note: If you use a separate declaration (prototype), put the default values in the declaration only, not again in the definition.

Pass by value, by reference and by const reference

How a parameter is declared decides whether the function receives a copy or works on the original:

cpp · try it yourself
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void byValue(int x)       { x = 100; }
void byReference(int& x)  { x = 100; }

// read-only access to a large object without copying it
double average(const vector<double>& v) {
    double sum = 0;
    for (double d : v) sum += d;
    return v.empty() ? 0 : sum / v.size();
}

int main() {
    int a = 1, b = 1;
    byValue(a);
    byReference(b);
    cout << "a=" << a << " b=" << b << "\n";

    vector<double> marks = {7.5, 9.0, 6.5, 8.0};
    cout << "average: " << average(marks) << "\n";
}
a=1 b=100
average: 7.75

Returning several values

A function returns one value — but that value can be a struct or a std::pair/std::tuple. Combined with structured bindings (C++17) this reads very naturally. Output parameters (references) are the older alternative:

cpp · try it yourself
#include <iostream>
#include <utility>
using namespace std;

pair<int, int> divide(int dividend, int divisor) {
    return {dividend / divisor, dividend % divisor};
}

void minMax(const int arr[], int n, int& lo, int& hi) {
    lo = hi = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] < lo) lo = arr[i];
        if (arr[i] > hi) hi = arr[i];
    }
}

int main() {
    auto [q, r] = divide(47, 5);
    cout << "47 / 5 = " << q << " remainder " << r << "\n";

    int data[] = {12, -4, 33, 8};
    int lo, hi;
    minMax(data, 4, lo, hi);
    cout << "min=" << lo << " max=" << hi << "\n";
}
47 / 5 = 9 remainder 2
min=-4 max=33

Passing arrays

A built-in array parameter decays to a pointer, so the function cannot know the length — pass it separately, as minMax does above. With std::vector or std::array the size travels with the object, which is one more reason to prefer them.

Choosing a parameter style

You want to… Write
read a small value (int, double, char, bool) T x
read a large object (string, vector, struct) const T& x
modify the caller's variable T& x
accept "maybe nothing" T* x (check for nullptr)

Test yourself

Which declaration is valid?

  1. void f(int a = 1, int b);
  2. void f(int a, int b = 2);
  3. void f(int a = 1, b);
  4. void f(default int a);

After calling void f(int x) { x = 5; } with int n = 1; f(n);, what is n?

  1. 5
  2. 1
  3. 0
  4. Undefined

What does auto [q, r] = divide(47, 5); use?

  1. Lambdas
  2. Structured bindings
  3. Templates
  4. Pointers