Module 18: Decisions and Functions
Functions
🏠The everyday picture
A recipe card titled "Masala chai". Once written, you don't re-explain the steps each time; you say "make masala chai", and anyone can follow it. A function is a named recipe in code.
📘What it really is
- A function is a named block of code you can run (call) whenever you need it:
function greet() {
console.log("Welcome to Tutor Workstation");
}
greet();
greet();
- Defining a function doesn't run it; calling it with
()does. - Why functions matter:
- reuse: write once, use many times;
- names explain intent:
calculateFee()says what the code is for; -
small pieces are easier to understand, test and fix.
-
A good function does one clear job and is named with a verb:
showMenu,checkForm,calculateTotal. - When reading AI code, start with the function names. They're the table of contents.
🖱️Try it
Move your level-finding code from Lesson 1 into a function called showLevel. Call it three times. Notice that you
can't yet tell it which number to use; that's the next lesson.
✅Quick check
You write a function but nothing happens. What did you probably forget?
Show the answer
To call it, with its name followed by ().
Why is calculateTotal a better function name than doStuff?
Show the answer
It says what the function does, so code that uses it reads clearly.
