Skip to main content

Defining and Calling Functions

beginner11 min readLesson 17 of 204

The anatomy of a function, why return beats print for graded logic, and single-responsibility sizing.

The shape

double bmi(double weight_kg, double height_m) {
    return weight_kg / (height_m * height_m);
}

int main() {
    std::cout << bmi(70.0, 1.75) << '\n';   // call with arguments
}

Read the first line as a contract: "given a weight and a height, I give back a double". The parameters (weight_kg, height_m) are the function's local variables, initialized from the caller's arguments. return hands the value back and ends the function immediately.

Return values, not printed values

Beginners often print inside a function and return nothing:

void bad(double w, double h) {
    std::cout << w / (h * h);   // computed, shown... and GONE
}

bad computed a value nobody can use. A function that returns can be printed, stored, tested, and combined:

double b = bmi(70, 1.75);
if (b > 25) { /* ... */ }

This is also exactly how the platform grades: tests call your functions and check returns. Print for humans; return for programs.

void and early return

void means "returns nothing" โ€” a void function is a procedure, an action (print_report(), program()). Inside any function, return; exits early:

void greet(const std::string& name) {
    if (name.empty()) return;   // nothing sensible to do
    std::cout << "Hi, " << name << '\n';
}

Size discipline

A function does one thing, at a size you can read without scrolling. When you write a comment like // now validate the input inside a function, that comment is usually a new function struggling to be born. Decomposition practice comes in 5C.

Now practice

Functions Practice: ContractsWrite functions to spec: pure computations, a const& reader, and one out-parameter fix.1 challenge ยท ยท ~25 min