Hey there! Today we are going to explore functions, which are like secret recipe machines in coding.
In coding, a function is a named block of code that does a specific job. Instead of writing the same steps over and over, you write them once inside a function and call its name whenever you need it.
Think of a function like a juice blender. You put fruit in, the blender runs its code, and you get delicious juice out! The fruit you put in is called an input, or a parameter.
Functions help us organize our code into smaller, neat subproblems. For example, one function can handle drawing a character, while another handles scoring points.
✏️ Worked example
Let's write a function called greetUser that takes a person's name as an input and prints a friendly greeting.
First, we define the function using a keyword like 'function' or 'def' followed by the name: greetUser.
Next, we add a parameter inside parentheses to hold the input, like this: greetUser(name).
Then, we write the action inside the function: print('Hello, ' + name + '!').
Finally, we call the function with a real name, like greetUser('Sam'), which outputs 'Hello, Sam!'.
Want the full interactive lesson — quiz, animations, and Finn cheering them on?