Functions

Return Values

Functions can send data back using

return
.

Basic Return

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

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

Return vs Print

# Print: Shows on screen, can't be used later
def add_print(a, b):
    print(a + b)

# Return: Sends value back, can be used
def add_return(a, b):
    return a + b

x = add_return(5, 3)  # x is 8
y = add_print(5, 3)   # y is None!

Multiple Returns

def get_user_info():
    return "Alice", 25, "[email protected]"

name, age, email = get_user_info()

Return Stops the Function

def check_age(age):
    if age < 0:
        return "Invalid age"  # Function stops here
    return f"Age is {age}"
Visualizer
function
calculate_area()
5
4
20

Interactive Visualization

python
Loading...
Output
Run execution to see output...