Skip to content
Functions
step 1/5

Reading — step 1 of 5

Learn

~2 min readFunctions and References

Functions

C++ functions start where C's left off — same value-copy semantics, same declare-before-use rule — then add two conveniences (overloading, default arguments) and set up the one big idea the next lesson delivers: reference parameters. This lesson is the launchpad.

The shape

int square(int n) {
    return n * n;
}

double square(double x) {      // OVERLOADING: same name, different parameter types
    return x * x;
}

Return type, name, typed parameters — and unlike C, two functions may share a name if their parameter lists differ. The compiler picks by argument type: square(7) calls the int version, square(2.5) the double one. The standard library leans on this everywhere (std::abs, std::max work on every numeric type); your own overloads should mean the same operation on different types — overloading print to sometimes email someone is legal and unforgivable.

Default arguments trim call-site noise:

void greet(std::string name, std::string tag = "Hello") {
    std::cout << tag << ", " << name << "\n";
}
greet("Ada");            // Hello, Ada
greet("Ada", "Howdy");   // Howdy, Ada

Defaults must be rightmost (all-or-nothing from the right), and they're resolved at the call site — think of them as pre-filled arguments, not function state.

Copies, still

void tryToDouble(int n) { n *= 2; }     // modifies its private copy

int x = 5;
tryToDouble(x);
std::cout << x << "\n";                 // 5 — unchanged

Arguments pass by value: the function gets copies, the caller's variables are safe. For an int, copying is free and the isolation is a feature. For a std::string holding a novel, or next lesson's vector of a million elements, copying is real cost — and "how do I avoid the copy without losing the safety?" is precisely the question references answer next lesson. For today, when a function should change something: return the new value. x = doubled(x); — visible data flow, no surprises.

Declare-before-use also survives from C: main can only call functions the compiler has already seen — define helpers above main, or give a prototype (int square(int);) up top and define below. Headers, when you meet multi-file projects, are files of prototypes; the rule is why.

Your exercise: Square It

int square(int n) (consider long long if inputs run large — 50,000² already exceeds int), plus a main that reads, calls, prints:

int square(int n) {
    return n * n;
}

int main() {
    int n;
    std::cin >> n;
    std::cout << square(n) << "\n";
    return 0;
}

Keep the platform's standing discipline — logic in functions, I/O in main — and note how little C++ asks of you here: no &, no format specifiers, the stream handles types. The simplicity is the point; next lesson complicates parameter-passing productively, and it lands better with this clean baseline in muscle memory.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…