Lesson 18 of 60
โฎPrev Nextโฏ

PY-101 ยท Functions

Defining functions

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

18of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Defining functions, running
    1. def show_banner():
    2. print("*********")
    3. print("* HELLO *")
    4. print("*********")
    5. show_banner()
    6. print("Welcome back!")
    7. show_banner()
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideadef name():A function is a named block of code you can run whenever you need it, by calling its name with brackets.
    3. 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()

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Web apps have functions like send_welcome_email() that dozens of features call, so the email looks the same everywhere.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Which keyword creates a function?
    2. 2. What does defining a function do on its own?
    3. 3. How do you call a function named go?
    4. 4. What must follow def name()?
    5. 5. Best function name?

    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