Return  Statement

Return Statement

Return Statement

Return  Statement: 

In Python, the return statement is used to exit a function and return a value to the caller. Here’s a simple example:

				
					def add_numbers(num1, num2):
    sum = num1 + num2
    return sum 

				
			

In this example, the add_numbers function takes two arguments num1 and num2 and returns their sum. The return statement is used to return the value of sum to the caller.

If a function doesn’t have a return statement, it will still return a value, but the value will be None. Here’s an example:

				
					def greet(name):
    print(f"Hello, {name}!")
result = greet("Alice")
print(result)  # Output: None

				
			

In this example, the greet function takes a name argument and prints a greeting message to the console. Since the function doesn’t have a return statement, it returns None. When we call the greet function and store the result in the result variable, the value of result is None.

Join To Get Our Newsletter
Spread the love