Studyfin Studyfin ✨ Sign up free

Functions

Grade 7 · Coding · Free lesson

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.

dyslexia

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.

The Function Machine Input (Fruit) → Function → Output (Juice)

Functions help us organize our code into smaller, neat subproblems. For example, one function can handle drawing a character, while another handles scoring points.

drawPlayer() movePlayer() keepScore() Breaking a big game into small functions!
✏️ Worked example

Let's write a function called greetUser that takes a person's name as an input and prints a friendly greeting.

  1. First, we define the function using a keyword like 'function' or 'def' followed by the name: greetUser.
  2. Next, we add a parameter inside parentheses to hold the input, like this: greetUser(name).
  3. Then, we write the action inside the function: print('Hello, ' + name + '!').
  4. Finally, we call the function with a real name, like greetUser('Sam'), which outputs 'Hello, Sam!'.
board
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons