Functions and return values
Separate reusable calculation from displaying a result.
What you’ll learn
- Define a function with parameters
- Distinguish print from return
- Test ordinary and boundary cases
Give a repeated operation a name
Suppose several parts of a program calculate the area of a rectangle. Repeating width multiplied by height everywhere makes it harder to change validation or understand what the expression represents. A function named rectangle_area gives that operation a clear entry point. Its parameters describe the information it needs, and its returned value describes what it produces. A useful function usually has one understandable responsibility. Start with a direct calculation before introducing abstractions for situations you have not encountered.
Parameters receive arguments
In a function definition, width and height are parameter names. In a call such as rectangle_area(4, 3), the values four and three are arguments supplied for those parameters. The function body has its own local names. Reassigning a local parameter does not reassign a caller's variable, although mutating a shared mutable object can affect what the caller sees. For beginner numeric functions, keep the inputs explicit and avoid reaching into unrelated global state to obtain necessary values.
Return a value to the caller
Returning and printing serve different purposes. print() communicates through an output stream, usually the terminal. return provides a value to the code that called the function and ends that function call. A caller can store the returned area, compare it, include it in another calculation, or display it later. If execution reaches the end of a function without an explicit return, its result is None. A function that only prints a number therefore cannot be used as if it returned that number.
Choose a clear input contract
Decide which values your function accepts. For a rectangle, do zero-sized dimensions make sense? Should a negative dimension be rejected? You do not need elaborate validation for every tiny exercise, but the assumptions must be understandable. A default parameter can make an optional choice convenient, such as a greeting prefix. Mutable defaults require care because the same default object is reused across calls. Prefer a simple interface whose behavior can be described in a sentence.
Test behavior independently
Try the function with a normal rectangle, a square, a zero dimension, and an invalid negative value if your contract rejects it. Predict each answer before running the code. Keeping calculation separate from input() and print() makes these checks easier because the function can be called with values directly. If a result is wrong, reduce the example until you can explain every step. Only then connect the function to a terminal prompt, file reader, or web interface.
Try it yourself
Describe a total_price(quantity, price) function that another function can reuse. Explain why it should return a value.
Show a possible solution
Return quantity multiplied by price, with a documented rule for invalid quantities or prices. The caller can then add shipping, compare totals, or print the answer without parsing terminal output.
Check your understanding
Try 14 questions across python and practical programming. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.