Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
Why Functions?Parameters & ArgumentsReturn StatementsScopeDefault ParametersVariable Arguments (*args)Lambda FunctionsDecoratorsFunctional ProgrammingBest Practices
Python/Functions/Return Statements

📤 Return Statements — Getting Results Back

Return statements let functions send data back to you so you can use the result.


🎯 What Does Return Do?

The `return` keyword sends a value back from the function so you can capture and use that value elsewhere in your code. Without `return`, your function would have no way to give you data back. Think of `return` as the function's way of saying "here's the result I calculated."

def add(a, b):
    return a + b

result = add(5, 3)  # result = 8
print(result)       # Output: 8

The function calculates 5 + 3 and returns the answer (8). You store it in a variable to use it.


📤 Return vs Print

These are NOT the same:

# print() — just shows it on screen
def add_print(a, b):
    print(a + b)

add_print(5, 3)  # Shows: 8
result = add_print(5, 3)  # Shows: 8, but result = None (nothing!)

# return — sends the value back
def add_return(a, b):
    return a + b

result = add_return(5, 3)  # result = 8 (we can use it!)
print(result)  # Output: 8

💡 Simple Example

Here's an easy example: the function takes a number, performs a calculation, and sends the result back to you. You capture that result in a variable and can then use it in any way you want—print it, do math with it, or pass it to another function.

def double(x):
    return x * 2

answer = double(5)
print(answer)  # Output: 10

The function takes 5, doubles it (10), and returns the result. You store it in `answer` and can use it.


🎨 Real-World Example

def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    return fahrenheit

temp = celsius_to_fahrenheit(0)
print(f"0°C is {temp}°F")  # 0°C is 32.0°F

temp2 = celsius_to_fahrenheit(100)
print(f"100°C is {temp2}°F")  # 100°C is 212.0°F

✅ Return Rules

1. `return` stops the function immediately

2. Code after `return` doesn't run

3. You can return any type of data

def get_name():
    return "Alice"  # Return a string

def get_age():
    return 25  # Return a number

name = get_name()
age = get_age()
print(f"{name} is {age} years old")  # Alice is 25 years old

🔑 Key Takeaways

ConceptRemember
returnSends data back from function
vs printprint shows data, return sends it back
Store resultUse `variable = function()`
Exit functionreturn stops the function

🔗 What's Next?

Now let's learn about scope — where variables can be used.

Next: Local vs Global Scope →


Ready to practice? Try challenges or view solutions


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn