Skip to content
Justice Tech School

Reference and Default Parameters

Pass by reference (&) to avoid copies and to change the caller variable. const & passes big objects cheaply. Default values and overloads add flexibility.

Example
#include <iostream>
#include <string>
using namespace std;
void inc(int &x) { x++; }
void show(const string &s, int times = 2) { for (int i = 0; i < times; i++) cout << s << " "; cout << "\n"; }
int area(int w, int h) { return w * h; }
double area(double r) { return 3.14 * r * r; }
int main() {
    int n = 5; inc(n); cout << n << "\n";
    show("hi"); show("yo", 3);
    cout << area(2, 3) << " " << area(1.0) << "\n";
}

Result

6
hi hi 
yo yo yo 
6 3.14

Exercise

Pass by reference.

void f(int  x)

Quiz Test

What does const & avoid?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.