Python Functions and def Explained for Beginners

Write it once, call it as many times as you like — no retyping.

  • Lesson 1 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

def gives your code a name

  1. The dragon: You've been writing print() lines one at a time. Functions let you name a whole GROUP of lines and reuse it.

  2. The dragon: def roar(): starts a new ability called "roar". Everything indented under it belongs to that ability.

    example.pyPython 3
    def roar():    print("ROAR!")
  3. The dragon: But def alone doesn't DO anything — it just defines the ability. You have to actually CALL it.

    example.pyPython 3
    def roar():    print("ROAR!")roar()
    OutputROAR!
  4. The dragon: And you can call it as many times as you want, without ever rewriting the print() line inside it.

    example.pyPython 3
    roar()roar()roar()
    OutputROAR! ROAR! ROAR!

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    def roar(): print("ROAR!") — with NO call anywhere after it. What happens when you run this?

    question.pyPython 3
    def roar():    print("ROAR!")
    • Nothing prints — defining an ability doesn't run itCorrect
    • ROAR! prints once, automatically

    Why: Nothing prints. def roar(): ... just teaches Python the ability called "roar" — it has to be CALLED with roar() to actually run.

  2. Question 2 of 2

    roar() is called THREE separate times in your code. How many times does ROAR! print?

    question.pyPython 3
    roar()roar()roar()
    • Three times — once per callCorrect
    • Once — it's the same ability, so it only really happens once

    Why: Three times. Every call runs the function's whole body again, from the top — calling it more than once means it happens more than once.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    def roar(): print("ROAR!") — defines an ability called roar. Call it TWICE by writing roar() twice, to make the dragon roar two separate times.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.