PY-101 ยท Functions
Parameters and return values
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
def area(width, height):return width * heightroom = area(4, 3)hall = area(6, 2)print("Room:", room)print("Total:", room + hall)
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
Room: 12
Total: 24 ๐ฏ Write celsius_to_f(c) that returns c * 9 / 5 + 32. Print celsius_to_f(100), which should be 212.0.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
return c * 9 / 5 + 32
- 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.
- The Python idea
def name(param1, param2):Parameters are the names in a function's brackets that receive the values you pass in, called arguments. - 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) Real-world scenario
A shipping service has calculate_shipping(weight, distance) that every part of the checkout calls and uses the returned price.
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: Default and keyword arguments โ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
printshows a value on screen;returnhands 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
- Defining functions (docs.python.org)