Functions and Modular Programming

Lesson 5 of 20

Package work into reusable blocks: parameters in, return values out — and one name wearing many overloads.

The big picture

Name a block of work

A function is packaged work: give it a name, hand it inputs, get an answer back — reuse it forever.

Why functions

Write a block once, call it everywhere — no copy-paste, one place to fix bugs.

Parameters

The inputs a function receives: square(int n) gets n from the caller.

Return value

return sends the result back to the caller and ends the call immediately.

Overloading

Same name, different parameter lists — C++ picks the version by your arguments.

Modular thinking

Small single-job functions are easier to read, test and fix than one giant main.