Skip to content
dreamcode
dreamcode
Map
Functions
Lesson 20 of 77
+15 XP on finish
PYTHON BASICSChapter 4 · Functions

Wrap it in a name

A function is a named bundle of steps. Define it once with def, then call it by name whenever you need it. Functions can take inputs in the parentheses and hand back a result with return.

Worked example

How it reads

  • def greet(name): defines a function called greet that takes one input, name
  • The indented body runs only when you call the function
  • return hands a value back to whoever called it
Cloud tip: Define a function once, call it as many times as you like. That is how you stop repeating yourself.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Write a function square(n) that returns n times n, then print square(9). It should print 81.

Press Run to check your work.