Module 06 / 15

Functions: The Magic Machines

Stop repeating yourself and start building tools.

Think about a Juicer. You don't need to know how the blades spin or how the motor works. You just put in a fruit, press a button, and get juice out. In coding, that Juicer is a Function.

What You Will Learn
  • What a Function is and why we use them.
  • How to give "Ingredients" (Parameters) to a function.
  • How to get a "Result" (Return) back from a machine.
Who Is This For?

This is for the builder who wants to be organized. Instead of writing 100 lines of messy code, you’ll learn to build small, neat machines that do specific jobs perfectly.

Real Life Connection

Think about a Toaster:

  • Input: Plain Bread 🍞
  • Process: The Toaster heats it up.
  • Output: Delicious Toast! 🥪

You can use the Toaster every morning. You don't build a new toaster every time you want breakfast—you just use the one you already have!

The Function Recipe

A function has three parts:

  1. The Name: Like makeToast.
  2. The Inputs: Called Parameters. They go in the ().
  3. The Output: Called Return. It’s the final result the machine gives back.
The Machine Flow
Input
🍎
THE MACHINE
(Function)
Output
🧃
Building Your Machine

Let's build a machine that takes a name and makes a "Welcome" greeting.

function createGreeting(studentName) {
  // studentName is our input ingredient
  const message = "Welcome to class, " + studentName;
  // We give the finished message back
  return message;
}
The "Magic Calculator" Script

Copy this into your editor. It has a machine that adds two numbers together!

function runMyApp() { // We use the machine here const result = addNumbers(10, 5); Logger.log("The answer is: " + result); } function addNumbers(num1, num2) { // This is the machine's internal logic return num1 + num2; }
The Result

When you run runMyApp, it calls the addNumbers machine. The machine does the math and sends back "15".

[Execution Log]
The answer is: 15
The Broken Machine
Click to see why your function might fail!

1. Forgotten Return: If you forget to type return, the machine finishes its work but throws the result in the trash! You'll get undefined.

2. Wrong Order: If your machine needs (bread, butter) and you give it (butter, bread), you might get a messy result. Order matters!

3. Missing Brackets: A function starts with { and ends with }. If one is missing, the whole workshop shuts down.

The "Square Machine" Challenge

Can you write a function called findSquare that takes one number and returns that number multiplied by itself? (Example: 5 * 5 = 25).

Who Else Uses This?

Amazon uses functions for everything! They have a calculateTax function that runs millions of times a day. They don't write the tax rules for every customer—they just "Call" the function.

Module Summary

You’ve learned to build your own tools! Functions make your code clean, reusable, and powerful. You are no longer just writing lines; you are building a factory of logic.

Status: Factory Architect 🏭 | Level: Modular Coder 🧩

Type NEXT to unlock the next module 🚀