Loops & Functions

Loops repeat actions automatically, and functions are reusable sets of instructions — together they make programs shorter and smarter.

Reading is good — doing is better. Practice Loops & functions as an interactive lesson.

Try the lesson

Definition

A loop is a block of code that runs over and over until a condition is met or it has repeated a set number of times. A function is a named group of instructions you write once and can use again and again anywhere in your program. Loops save you from copying the same line dozens of times, and functions save you from rewriting the same steps every time you need them.

Remember the rule

DRY — Don't Repeat Yourself. If you write the same steps more than twice, use a loop or a function!

Key words

Loop
A command that makes the computer repeat steps multiple times without you writing them out each time.
Function
A named set of instructions you create once and can call whenever you need it.
Iteration
One single time through a loop — if a loop runs 5 times, it has 5 iterations.
Parameter
A piece of information you pass into a function so it can do its job, like giving it a number to work with.
Call
When you tell the program to run a function you already defined, you are calling it.
Condition
A true or false test that a loop checks to decide if it should keep going or stop.
Repeat block
A simple loop in Scratch or block coding that runs its inside steps a fixed number of times.
Define
To write the instructions for a function for the first time, giving it a name and its steps.

Worked examples

You want to print 'Hello!' five times. Without a loop you write: print('Hello!') five separate times. How would a loop help?

Use a loop: for i in range(5): print('Hello!') — the computer runs that one line 5 times automatically. · The loop saves you from writing the same line over and over and makes it easy to change — just change 5 to any number.

In Scratch, you want a sprite to move 10 steps and then turn 90 degrees four times to draw a square. How do you use a loop?

Put 'move 10 steps' and 'turn 90 degrees' inside a 'repeat 4' block. The two actions run 4 times, drawing all four sides. · Without the loop you would need 8 separate blocks; the loop cuts it to 3 blocks total.

You write a function called greet() that prints 'Welcome to class!' You want to use it at the start and end of the program. How do you do that?

Define it once: def greet(): print('Welcome to class!') — then call greet() at the top and greet() at the bottom. · The message only needs to be typed inside the function once, even though it appears twice in the program.

A function called double(number) is supposed to give back twice the value passed in. What does double(6) return?

double(6) returns 12, because the function multiplies its parameter by 2: 6 × 2 = 12. · The word 'number' is the parameter — it gets replaced by 6 when you call the function.

A loop is set to run while a counter is less than 4, starting at 0. How many times does it run, and what are the counter values?

It runs 4 times. The counter values are 0, 1, 2, and 3 — when it reaches 4 the condition is false and the loop stops.

You need a program to add a star border of 10 stars on each of 6 lines. Should you use a loop, a function, or both?

Use both: write a function called starLine() that has a loop printing 10 stars, then call starLine() 6 times in an outer loop. · Combining loops and functions is very common — it keeps code neat and easy to read.

Common mistakes

  • Forgetting to change the counter inside a loop, so the condition is always true and the loop runs forever — called an infinite loop.
  • Defining a function but never calling it, then wondering why nothing happens.
  • Starting a loop counter at 1 instead of 0 and getting one extra or one fewer iteration than expected.
  • Putting code outside the loop that should be inside, so it only runs once instead of repeating.
  • Mixing up defining a function (writing its steps) with calling a function (running those steps) — you must define first, then call.

FAQs

What is the difference between a loop and a function?

A loop repeats the same steps right there in your code a set number of times. A function stores steps under a name so you can run them from different places in your program whenever you want.

Can a loop be inside a function?

Yes! A function can contain a loop. When you call the function, the loop inside it runs. You can also call the same function in a loop to repeat it many times.

What happens if my loop never stops?

You get an infinite loop — the program keeps running forever and usually freezes. Always make sure your loop has a condition that will eventually become false, or a fixed repeat count.

Do functions have to give back an answer?

No. Some functions just do something, like printing text. Others use the word 'return' to send an answer back to the rest of the program, like returning the result of a math calculation.

How does the computer know where a function's instructions end?

In block coding, the blocks inside the function's define hat are grouped together. In text coding like Python, indentation — the spaces at the start of each line — shows what belongs inside the function.

Why should I bother with functions if I can just copy and paste my code?

If you copy and paste and then find a mistake, you have to fix it in every single place you pasted it. With a function, you fix it once inside the function and every call is automatically fixed.

Want the whole picture for your child?

Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.

Start a 3-day free trial

Related concepts (5th Grade Technology)