Module 13 of 15 · Step 1 of 14

Functions

A function gives a reusable group of instructions a name. You can call it whenever you need that work, and it can return a value for the rest of the program.

By the end of this lesson, you can:
  • Define and call a function
  • Pass arguments into parameters
  • Explain return versus print
  • Use two parameters and a local variable
Functions lesson progress0%

Course progress: 0% · 0 of 14 steps completed in this module

Learn · Name a reusable action
Concept 1 of 5

Name a reusable action

def begins a function definition. The body is indented. Defining it does not run it; you must call the function by writing its name followed by parentheses.

Pattern
def say_hi():
    print("Hi")

say_hi()
Code
def say_hi():
    print("Hi")

say_hi()
Output
Hi

Why it works

  1. def stores the action under say_hi.
  2. The indented line belongs to the function.
  3. The call say_hi() makes its body run once.

Common mistake

Not this
def say_hi():
    print("Hi")  # never called
Try this
say_hi()  # call after defining

A definition alone produces no output.