Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
return Statement, Multiple Returns, & NoneParameters allow you to send data into a function. The return statement is the mechanism that sends computation results back out to the caller.
Executing return accomplishes two simultaneous actions:
+-------------------------------------------------------------------------------+
| RETURN VALUE REAL-WORLD ANALOGIES |
+-------------------------------------------------------------------------------+
1. THE STATE BANK ATM CASH DISPENSER:
- You insert your debit card and type PIN and amount: Rs 10,000.
- The ATM processes the transaction internally.
- Cash slot mechanical door opens and yields crisp rupee notes:
`return [Note500, Note500, ...]`
- If the ATM merely displayed "Success" on the screen without ejecting cash
(like `print()` without `return`), you would leave empty-handed!
- The physical cash handed to you is the **Return Value**.
2. DR. LAL PATHLABS DIAGNOSTIC BLOOD TEST:
- You submit a blood vial sample (argument).
- The analyzer runs biochemical diagnostics.
- The laboratory issues an official health report containing three metrics:
`return hemoglobin, blood_sugar, platelet_count`
- A single diagnostic procedure returns **multiple values** packaged together.
+-------------------------------------------------------------------------------+print() vs. returnA pervasive beginner misconception is confusing print() with return:
returnThe syntax of a return statement is:
When Python reaches return, <expression> is evaluated and returned immediately. Any code written after an unconditional return is unreachable dead code:
How data and code flow step-by-step
Output:
Many programming languages (such as Java or C) restrict functions to returning only a single value. Python allows returning multiple values separated by commas:
Output:
Under the hood, Python packs comma-separated return items into an immutable tuple.
None ReturnEvery Python function returns a value. If you do not include an explicit return statement, or write a bare return with no argument, Python implicitly returns None:
Output:
The return statement is widely used as a guard clause to handle invalid inputs or boundary conditions upfront without nesting code inside deep else blocks:
Output:
| Best Practice (DO) | Dangerous Anti-Pattern (DON'T) |
|---|---|
DO use return to send data back to the calling code for further processing. | DON'T confuse print() with return; print() outputs text to the console but returns None. |
DO unpack returned tuples directly into descriptive variable names: min_val, max_val = get_bounds(). | DON'T return 6+ comma-separated values; return a dict or dataclass for readability. |
DO use early return statements (guard clauses) to exit functions as soon as an error is detected. | DON'T write code beneath an unconditional return statement, as it will never execute (dead code). |
return statement exits a function and hands a value back to the caller.return return None by default.return a, b, c) packages them into an immutable tuple.x, y, z = get_metrics().print() displays text on the screen; return provides usable data to your program.return StatementIf a function computes a value but doesn't return it, Python returns None by default:
global KeywordReading a global variable is permitted, but assigning to it creates a local variable unless explicitly declared:
return statement?A. 0 B. False C. None D. An empty string ""
return 10, 20, 30?A. list B. tuple C. dict D. set
A. 20 \n None B. 20 \n 20 C. 20 D. None \n None
return statement?A. Python executes them during garbage collection B. They are ignored and will never execute (dead code) C. Python throws a SyntaxError: unreachable code D. They execute in reverse order
def get_user(): return "Aarav", 25, "Delhi"?A. user = get_user()[all] B. name, age, city = get_user() C. name & age & city = get_user() D. [name, age, city] -> get_user()
Build an automated salary slip calculation engine for an Indian technology corporation (Infosys / TCS).
Create a function: calculate_salary_breakdown(employee_id, employee_name, basic_salary_inr, bonus_inr=0.0)
gross_salary, total_deductions, net_salary, and itemized_deductions).In the next lesson, we will continue your Python learning journey with Lambda Functions (14: Functions).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Arguments: Default, *args, **kwargs | Lambda Functions |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.