Q.What does the return statement do in a function ? Explain with the help of an example.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The return statement sends a value back from a function to the caller and immediately exits the function. Without it, a function returns None by default.
The Core Idea
Think of a function like a vending machine. You put in money (arguments), press a button (call the function), and the machine gives you a product (the return value). The return statement is the mechanism that actually hands you that product. Without it, the machine might do work inside — lights flash, gears turn — but nothing comes out for you to hold.
In programming, when you call a function, you usually want it to compute something and give you the result. The return statement does two things at once:
- It sends a value back to the place where the function was called.
- It immediately stops the function — no further code in the function runs after
return.
If a function has no return statement, Python still returns something: the special value None. This is like the vending machine saying "here's nothing" — technically a response, but not useful.
Step-by-Step Explanation
-
What
returndoes in a functionWhen Python encounters
returninside a function, it evaluates the expression afterreturn(if any), then hands that value back to the caller. The function's execution ends right there — any lines afterreturnare ignored. -
The default return value
If a function ends without hitting a
returnstatement, Python automatically returnsNone. This is a common source of bugs for beginners: they write a function that prints a result but forget to return it, then wonder why the variable they assigned getsNone. -
Example: A function that returns a value
Consider a function that calculates the area of a rectangle:
def rectangle_area(length, breadth): area = length * breadth return areaWhen you call
result = rectangle_area(5, 3), the function computes15, andreturnsends that15back. Nowresultholds15. You can use it in further calculations. -
Example: A function without
returnCompare with this version:
def rectangle_area(length, breadth): area = length * breadth print(area)Calling
result = rectangle_area(5, 3)prints15to the screen, butresultbecomesNone. The function did the work but didn't hand back the value — it only showed it. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.