Watch on Instagram

Code Bytes · "Intro to Programming" · Episode 12 · Functions and lists

Functions

A function is a named chunk of code, like a recipe card with a name. You write it once with def, and it runs each time you call it by name with brackets, such as greet(). Defining a function only stores it; calling it runs it.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

Write once, use everywhere

Imagine you need the same few lines in five places in your program. You could copy and paste them five times, but then every fix has to be made five times too.

A function solves that. Think of it as a recipe card with a name. You write the steps on the card once. After that, whenever you want the dish, you just say the name of the recipe.

Writing a function: def

``python def greet(): print("Hi there!") print("Welcome!") ``

Piece by piece:

  • def is short for define. It tells Python "I'm making a function".
  • greet is the function's name. You choose it, just like a variable name.
  • () are brackets. They're empty for now. (Next episode we put things inside them.)
  • : the colon says "the steps come next".
  • The two indented lines underneath are the steps. Indented means pushed in by 4 spaces, the same way the lines inside an if or a loop are.

The surprise: defining doesn't run it

If you run just those three lines, nothing prints at all. Really: the output is empty.

That's because def only stores the recipe card. Python reads it, remembers that there is now a function called greet, and moves on. The steps inside wait until someone asks for them.

Calling a function

To run the steps, you call the function: write its name followed by brackets.

```python def greet(): print("Hi there!") print("Welcome!")

greet() greet() ```

`` Hi there! Welcome! Hi there! Welcome! ``

Each greet() makes Python jump up into the function, run both steps in order, then come back and carry on with the next line. Two calls, so both lines print twice.

The brackets matter. greet() runs the function. Plain greet with no brackets just names it and runs nothing.

Edit once, every call changes

Here's the real payoff. Say you want the welcome message to be friendlier. You change the one line inside the function, and every place that calls greet() gets the new version automatically. One edit instead of five.

Try it yourself

Open any free online Python editor (search "online Python") and run this:

```python def say_bye(): print("Bye!") print("See you soon!")

say_bye() say_bye() say_bye() ```

You should see:

`` Bye! See you soon! Bye! See you soon! Bye! See you soon! ``

Now delete one of the say_bye() lines and predict the output before you run it again. Then try deleting all three calls: what prints now? (Nothing, because the function is only defined, never called.)

Good to know

  • Every snippet on this page was run with Python 3 and printed exactly what is shown.
  • You've been calling functions since your first line of code: print("Hello") is a call to a function that comes with Python. That's why it has brackets too.
  • The indented lines under def are called the function's body.
  • Python reads your file from top to bottom. A function has to be defined before the line that calls it runs, or Python says it doesn't know the name.
  • Next episode: parameters and return values, how to fill those empty brackets so one function can work with different values.

Answer to the comment question

What does this print?

``python def cheer(): print("Go!") print("Ready") cheer() ``

It prints two lines: Ready first, then Go!.

`` Ready Go! ``

The trap is reading top to bottom and expecting Go! first. But the def only stores the function cheer; nothing inside it runs yet. The first line that actually prints is print("Ready") (it isn't indented, so it's not part of the function). Then cheer() calls the function, which runs its one step and prints Go!.

Test your knowledge

Quick quiz: Functions

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What does def do in Python?
2You run only the three lines that define greet. What prints?
3How do you call (run) a function named greet?
4greet() prints two lines, and you call it twice. How many lines print in total?

Sources

Video transcript

How do you write code once, and use it everywhere?

You make a function. It's like a recipe card with a name. Write the steps once, then just say the name to cook it again.

In Python, we type def, short for define, then the name greet, empty brackets, and a colon. We'll fill those brackets next time.

Underneath, indented, go the steps: print Hi there, and print Welcome.

Here's the surprise: defining a function doesn't run it. Python just stores the recipe card. Nothing prints yet.

To run it, you call it. Type greet with brackets. Call it twice, and both lines print twice.

Need a change? Edit the function once, and every call gets it.

What does this print? Comment your answer below!

That's your byte.

More from "Intro to Programming"

All "Intro to Programming" lessons