Return values let a function send back a result after it finishes. This helps you reuse the result later in your program.
0
0
Return values in Python
Introduction
When you want a function to calculate something and give you the answer.
When you need to get data from a function to use somewhere else.
When you want to check if a function worked and get a status.
When you want to break a big task into smaller parts that return results.
When you want to avoid repeating code by using function results.
Syntax
Python
def function_name(parameters): # do something return value
The return keyword sends a value back to where the function was called.
Once return runs, the function stops running any more code inside it.
Examples
This function adds two numbers and returns the sum.
Python
def add(a, b): return a + b
This function returns a greeting message.
Python
def greet(): return "Hello!"
This function returns
True if a number is even, otherwise False.Python
def check_even(num): if num % 2 == 0: return True else: return False
Sample Program
This program defines a function that multiplies two numbers and returns the result. Then it prints the result.
Python
def multiply(x, y): return x * y result = multiply(4, 5) print(f"4 times 5 is {result}")
OutputSuccess
Important Notes
If a function does not have a return statement, it returns None by default.
You can return any type of value: numbers, text, lists, or even other functions.
Use return to get results out of functions instead of printing inside them for better reuse.
Summary
Return values let functions send results back to the caller.
Use return to stop a function and give back a value.
Returned values can be saved, printed, or used in other calculations.