PY-101 ยท Functions
Defining functions
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A function is a named block of code you can run whenever you need it, by calling its name with brackets. You create one with def, a name, brackets and a colon, then an indented body. Functions stop you repeating yourself and make programs easier to read.
- Define a function with def
- Call it more than once
- Name functions clearly
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
def show_banner():print("*********")print("* HELLO *")print("*********")show_banner()print("Welcome back!")show_banner()
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
*********
* HELLO *
*********
Welcome back!
*********
* HELLO *
********* ๐ฏ Define a function cheer() that prints "Go Python!" and call it three times.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
def cheer(): print("Go Python!"), then cheer() three times.
- The problemYour app shows the same welcome banner on five screens. Copying the code five times means fixing a typo five times. A function means fixing it once.
- The Python idea
def name():A function is a named block of code you can run whenever you need it, by calling its name with brackets. - In real lifeWeb apps have functions like send_welcome_email() that dozens of features call, so the email looks the same everywhere.
Start with a question
Your app shows the same welcome banner on five screens. Copying the code five times means fixing a typo five times. A function means fixing it once.
Every real program is built from functions. They are how you split a big problem into small, testable pieces.
Explain it like I'm 10
A function is a recipe card. Writing the card (def) does not cook anything. Cooking happens every time someone reads the card out (calls it).
The technical explanation
def show_banner(): creates the function. The indented lines under it are its body.
show_banner() calls it. Without the brackets you are only naming it, not running it.
Use verbs for names, in snake_case: send_email, print_report.
Syntax
def name():
...
name() Real-world scenario
Web apps have functions like send_welcome_email() that dozens of features call, so the email looks the same everywhere.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Parameters and return values โQuestions people ask
Why use functions in Python?
To avoid repeating code, to give a block a meaningful name, and to test pieces of a program on their own.
Can a function call another function?
Yes. Most programs are functions calling functions. A function can even call itself, which is called recursion.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Why use functions in Python?
To avoid repeating code, to give a block a meaningful name, and to test pieces of a program on their own.
beginner Can a function call another function?
Yes. Most programs are functions calling functions. A function can even call itself, which is called recursion.
intermediate Which keyword creates a function?
def. Python uses def.
intermediate What does defining a function do on its own?
Nothing visible until it is called. The body runs only when called.
Quick revision
Remember
- Define a function with def
- Call it more than once
- Name functions clearly
- Defining a function does not run it. If nothing happens, check that you actually called it with brackets:
show_banner().
Syntax
def name():
...
name() Official documentation
- Defining functions (docs.python.org)