🧩 Functions: A Recipe With a Name

5th Grade 💻 Coding

When your mom says “Clean your room,” she doesn’t list every step: pick up the clothes, put away the books, wipe the desk. “Clean your room” is a name for a whole list of actions. In programming, that’s called a function. In this lesson we learn to build our own functions, pass information into them and get an answer back.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

A name for a group of instructions

A function is a group of instructions that you give a name. Once you’ve defined it, you can “call” it — that is, run it — anywhere in the program, as many times as you like, just by writing its name. Instead of copying the same ten lines into five different places, you write them once inside a function and call it five times.

You’ve already used functions without knowing it: print in Python is a function that someone else wrote. When you write print("Hello"), you call the function print and hand it the text. Behind the scenes it does a lot of work so the word appears on the screen — and we don’t need to know how. That’s the power of a function: it hides the details and leaves just a clear name.

Building a function

In Python you define a function with the word def (short for “definition”). For example: def double(n): return n * 2. Here double is the name, n is the input the function receives, and return says what it gives back: n times 2. After that definition, print(double(5)) shows 10, and double(21) returns 42. Usually the body of the function goes on its own line, with spaces at the start.

In Scratch you build a function in the “My Blocks” category: make a new block, give it a name — say, “draw square” — and snap the instructions underneath it. From then on there’s a new pink block called “draw square,” and you can drag it into your program like any other block. You can even use it inside a loop and draw ten squares.

What goes in and what comes out

Many functions take in information from outside. That information is called a parameter, or an argument. In the function double, the parameter is n: it’s an empty slot that gets filled with a different value each time the function is called. A “draw square” function can take a “size” parameter, so the same function can draw a small square or a big one. Parameters turn one function into a flexible tool.

Some functions also give back an answer — a return value. double returns a number, and a function that checks whether a number is even could return true or false. You can think of a function as a machine: you put something in one side (the parameters), it does some work, and something comes out the other side (the return value). A juicer is a “function”: oranges go in, juice comes out.

Why programmers love functions

Functions keep things organized. Instead of one long, confusing program, you get small pieces with clear names: “start game,” “move player,” “check collision,” “show score.” Each piece can be tested on its own, fixed on its own and handed to a friend to work on. And when you find a bug in a function and fix it, it’s fixed everywhere the function is used.

There are also big collections of ready-made functions, called libraries. Python has a library for math, a library for random numbers, a library for drawing and more. Programmers don’t reinvent everything from scratch — they use functions that other people wrote and tested, and build on top of them. That’s how giant programs get built: small functions that use other functions, layer upon layer.

Key facts

  • A function is a named group of instructions that you can run again and again.
  • In Python you define a function with the word def and give back an answer with the word return.
  • A parameter is information that goes into a function; the return value is the answer that comes out.
  • print is a ready-made function that’s already built into Python.
  • In Scratch you create a function in the “My Blocks” category.

Quick quiz

  1. What is a function?

  2. Which word defines a function in Python?

  3. If def double(n): return n * 2, what does double(5) return?

  4. What is a parameter?

  5. What does the word return do?

  6. How do you make a function in Scratch?

  7. What’s the advantage of fixing a bug inside a function?

  8. What is a library in programming?

💭 Think and talk about it

  1. What “functions” are there in your morning? Give names to three groups of actions.
  2. Come up with a function for a game, with a name, a parameter and a return value.
  3. Why do you think it’s a good idea to split a big program into small functions?

Questions kids ask

What’s the difference between a function and a program?

A program is all the code that runs. A function is a small named part inside it that does one job. A good program is built from many functions that call one another.

Does every function have to return a value?

No. Some functions just do something — draw, move a character or show text — without giving back an answer. In Python, a function like that returns a special value called None, which means “nothing.”

What’s the difference between a parameter and an argument?

A parameter is the name in the function’s definition (n in double). An argument is the actual value you give when you call it (5 in double(5)). In everyday talk, people use the two words almost interchangeably.

🔎 Ask Thinkapedia another question