Learning Goals
- Coming soon: How to organize code with functions.
- Understand why repeated logic should be grouped.
- Prepare to read function syntax without pressure.
Core Logic
Functions help you organize code. We will cover this after you are comfortable with variables and if statements.
If you repeat the same steps many times, your code becomes long and hard to maintain. Functions help you write once and reuse.
console.log("Welcome, Patricia");
console.log("Welcome, Asha");
console.log("Welcome, Brian");
When we write similar lines like this, we mean the action is the same but one small value changes. A function will handle that for us in the next phase.
Functions are one of the biggest upgrades in programming because they reduce repetition and improve structure. Imagine your project grows from 20 lines to 500 lines. Without reusable blocks, editing one behavior means changing many places and risking mistakes. Functions solve this by giving one place to define behavior and many places to reuse it.
Even before writing full function syntax, your brain should already ask: "Is this logic repeating?" If yes, that is function territory. This habit will make your later lessons easier and your code cleaner from the beginning.
Create a file called function-thinking.js. Write 6 repeated lines that print report messages for different names, then add a comment explaining why a function would reduce the code.
console.log("Report ready for Patricia");
console.log("Report ready for Asha");
console.log("Report ready for Brian");
console.log("Report ready for Joel");
console.log("Report ready for Fiona");
console.log("Report ready for Maria");
// A function would avoid repeating the same print pattern.