Skip to content
  • Functions · C++

Functions

LearnViaNotes @learnvianotes· 3 min read

A function is a named, typed block of code: a return type, a name, a parameter list and a body. It must be declared before it is called.

Declaration vs definition

C++
#include <iostream>

int add(int a, int b);                 // declaration (prototype): signature only

int main() {
    std::cout << add(2, 3) << '\n';    // OK: the compiler has seen the declaration
}

int add(int a, int b) {                // definition: signature + body
    return a + b;
}
Output
5
PartExampleNotes
Return typeint, void, autovoid = returns nothing; auto = deduced from return (C++14)
NameaddCan be overloaded (same name, different parameters)
Parameters(int a, int b)Each is a local variable initialised from the argument
Trailing returnauto add(int a, int b) -> intUseful when the type depends on parameters
Specifiersinline, constexpr, static, noexcept, [[nodiscard]]Change linkage, evaluation or guarantees

Return values

C++
#include <iostream>
#include <string>
#include <utility>

auto average(double a, double b) { return (a + b) / 2; }          // deduced: double

std::pair<int, int> divmod(int a, int b) { return {a / b, a % b}; } // two results

[[nodiscard]] bool save(const std::string& path) { return !path.empty(); }

void greet(const std::string& who) {
    if (who.empty()) return;              // early return from a void function
    std::cout << "Hello, " << who << '\n';
}

int main() {
    std::cout << average(3, 4) << '\n';
    auto [q, r] = divmod(17, 5);
    std::cout << q << ' ' << r << '\n';
    greet("Ada");
    greet("");
    if (!save("out.txt")) std::cout << "failed\n";
}
Output
3.5
3 2
Hello, Ada

Function call mechanics

  1. Arguments evaluated

    In unspecified order, then copied/bound into the parameters.

  2. Stack frame pushed

    Parameters and locals live in the new frame (automatic storage).

  3. Body runs

    Until return or the closing brace.

  4. Frame popped

    Locals are destroyed (destructors run, in reverse order). The return value is handed back — often constructed directly in the caller's space (copy elision).

C++
#include <iostream>
int& broken() {
    int local = 42;
    return local;          // warning (error with -Werror): reference to stack memory returned
}
int main() { std::cout << broken() << '\n'; }

main and static locals

C++
#include <iostream>
int nextId() {
    static int counter = 0;   // initialised once, lives for the whole program
    return ++counter;
}
int main() {
    nextId(); nextId();
    std::cout << nextId() << '\n';
}
Output
3

What is copy elision / RVO?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report