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
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
- def stores the action under say_hi.
- The indented line belongs to the function.
- The call say_hi() makes its body run once.
Common mistake
Not this
def say_hi():
print("Hi") # never calledTry this
say_hi() # call after definingA definition alone produces no output.