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

PY-101 ยท Functions

Parameters and return values

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

19of 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

    Parameters are the names in a function's brackets that receive the values you pass in, called arguments. return sends a value back to whoever called the function, and ends the function. A function without return gives back None.

    • Give a function parameters
    • Return a result
    • Know print vs return

    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.

    Parameters and return values, running
    1. def area(width, height):
    2. return width * height
    3. room = area(4, 3)
    4. hall = area(6, 2)
    5. print("Room:", room)
    6. print("Total:", room + hall)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA tax calculator is useless if it only prints. The checkout needs the number back, so it can add it to the total and show it on the receipt.
    2. The Python ideadef name(param1, param2):Parameters are the names in a function's brackets that receive the values you pass in, called arguments.
    3. In real lifeA shipping service has calculate_shipping(weight, distance) that every part of the checkout calls and uses the returned price.

    Start with a question

    A tax calculator is useless if it only prints. The checkout needs the number back, so it can add it to the total and show it on the receipt.

    Functions that take inputs and return outputs can be reused anywhere and tested easily. That is how real libraries work.

    Explain it like I'm 10

    A juice machine: fruit goes in (parameters), juice comes out (return). The machine does not drink the juice; it hands it to you.

    The technical explanation

    def area(width, height): declares two parameters. area(4, 3) passes 4 and 3 as arguments.

    return width * height ends the function and gives the result back. Store it: room = area(4, 3).

    Arguments are matched to parameters by position: the first goes to the first parameter.

    Syntax

    def name(param1, param2):
        return result
    
    value = name(arg1, arg2)

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

    Real-world scenario

    A shipping service has calculate_shipping(weight, distance) that every part of the checkout calls and uses the returned price.

    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. In def add(a, b):, what are a and b?
    2. 2. What does a function return with no return statement?
    3. 3. def f(x): return x * 2. What is f(5) + 1?
    4. 4. What happens after return runs?
    5. 5. Why return instead of print?

    Questions people ask

    What is the difference between a parameter and an argument?

    A parameter is the name in the function definition (def f(x):). An argument is the actual value you pass when calling it (f(5)).

    Can a Python function return two values?

    Yes: return a, b returns a tuple, and you can unpack it with x, y = f().

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between a parameter and an argument?

    A parameter is the name in the function definition (def f(x):). An argument is the actual value you pass when calling it (f(5)).

    beginner Can a Python function return two values?

    Yes: return a, b returns a tuple, and you can unpack it with x, y = f().

    intermediate In def add(a, b):, what are a and b?

    Parameters. Names in the definition are parameters; values passed in are arguments.

    intermediate What does a function return with no return statement?

    None. Python returns None by default.

    Quick revision

    Remember

    • Give a function parameters
    • Return a result
    • Know print vs return
    • print shows a value on screen; return hands it back to your code. If you need to use the result later, you need return.

    Syntax

    def name(param1, param2):
        return result
    
    value = name(arg1, arg2)

    Official documentation