Aral Code

Functions & Data Lesson 9 of 17

C++ Functions — Overloading, Default Arguments and References

Write C++ functions with parameters and return values, overload them, give parameters default values, pass by reference to change variables, and use recursion.

Functions split a program into small named pieces. C++ functions work like C functions, with three extra features that make them more convenient: overloading, default arguments and pass-by-reference.

A basic function

#include <iostream>
using namespace std;

double average(double a, double b, double c) {
    return (a + b + c) / 3;
}

bool isPassing(double grade) {
    return grade >= 75;
}

int main() {
    double avg = average(78, 74, 70);
    cout << "Average: " << avg << endl;
    cout << boolalpha << "Passed: " << isPassing(avg) << endl;
    return 0;
}

Output

Average: 74
Passed: false

As in C, a function must be declared before it's called. Either define it above main, or put a prototype like double average(double, double, double); at the top.

Overloading

Several functions can share a name if their parameters differ:

#include <iostream>
#include <string>
using namespace std;

int area(int side) { return side * side; }
int area(int w, int h) { return w * h; }
double area(double radius) { return 3.14159 * radius * radius; }

int main() {
    cout << area(5) << endl;
    cout << area(4, 6) << endl;
    cout << area(2.0) << endl;
    return 0;
}

Output

25
24
12.5664

The compiler picks the version whose parameters match the arguments.

Default arguments

#include <iostream>
using namespace std;

double jeepFare(double km, bool discounted = false) {
    double fare = 13 + (km > 4 ? (km - 4) * 1.80 : 0);
    return discounted ? fare * 0.80 : fare;
}

int main() {
    cout << jeepFare(9) << endl;
    cout << jeepFare(9, true) << endl;
    return 0;
}

Output

22
17.6

Default values must come at the end of the parameter list.

Pass by value vs pass by reference

Normally, a function gets a copy of each argument. Add & to the parameter type to pass a reference: the parameter becomes another name for the caller's variable, so changes affect the original.

#include <iostream>
using namespace std;

void addBonusCopy(int score) { score += 5; }
void addBonusRef(int &score) { score += 5; }

void swapValues(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int s = 80;
    addBonusCopy(s);
    cout << "After copy: " << s << endl;
    addBonusRef(s);
    cout << "After reference: " << s << endl;

    int x = 1, y = 2;
    swapValues(x, y);
    cout << "x=" << x << " y=" << y << endl;
    return 0;
}

Output

After copy: 80
After reference: 85
x=2 y=1

References do what C needed pointers for, with simpler syntax: no & when calling and no * inside the function.

Const references for big objects

Copying a large string or vector is slow. Pass it by const reference: no copy, and the function promises not to change it.

#include <iostream>
#include <string>
using namespace std;

int countVowels(const string &text) {
    int n = 0;
    for (char c : text) {
        if (string("aeiouAEIOU").find(c) != string::npos) n++;
    }
    return n;
}

int main() {
    cout << countVowels("Ang pagbasa ay pagkatuto") << endl;
    return 0;
}

Output

9

Recursion

#include <iostream>
using namespace std;

long long fib(int n) {
    if (n < 2) return n;
    return fib(n - 1) + fib(n - 2);
}

int main() {
    for (int i = 0; i <= 10; i++) cout << fib(i) << " ";
    cout << endl;
    return 0;
}

Output

0 1 1 2 3 5 8 13 21 34 55

Lambdas (preview)

C++ also has small unnamed functions called lambdas, often passed to algorithms:

auto square = [](int n) { return n * n; };
cout << square(9);   // 81

You'll use them with sort and other STL algorithms later.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does void f(int &x) allow f to do?
2. Where must default arguments be placed?
3. What is function overloading?
4. How should you pass a large string that won't be changed?

By Sir Reyel · Last updated 2026-12-04