Lesson 9: Functions — definition, prototype & return
Until now all our code lived inside main, and when we wanted to repeat an idea — we copied lines. A function is a named block of code: you write it once and call it again and again, and each call can receive different values. The signature int add(int a, int b) says: the function add takes two ints
A function is like a vending machine: you insert coins (arguments), the machine works inside, and out comes a can (the return value). You don't need to know what happens inside — only what goes in and what comes out.
- function
- A named block of code that performs one task. Written once, called as many times as you like.
- parameter
- A variable in the function signature (like a and b) that receives the value the caller passes.
- prototype
- A declaration above main, like int add(int a, int b); — it tells the compiler the name, parameters and return type, without a body.
- return
- Sends a value back to the caller and immediately ends the function.
- void
- The return type of a function that returns no value — it just performs an action, e.g. printing.