Explain Return Statement in Python

Explain Return Statement in Python

A return statement signals the end of a function’s execution, sending back the result (the value of the expression following the return keyword) to the caller. Anything after the return statement won’t be executed. When a return statement lacks an expression, it returns the special value None. Ultimately, a return statement is how a function is activated, allowing the execution of the provided statements.

Note: Return statements are only used inside functions.

Syntax:

def tmp():
    statement
  return [ expression ]
Here, I've given you an example in which the return statement performs an operation and returns a value.

Example:

# Python program to demonstrate return statement

def add(x, y):
# returning sum of x and y
  return x + y
def is_true(x):
# returning boolean of x
  return bool(x)
# calling the function
tmp = add(2, 3)
print(f"Result of add function is {tmp}")
tmp = is_true(2<5)
print(f"\nResult of is_true function is {tmp}")

Output:

Here I gave you another example of which you can return multiple values using the object.

Example:


# A Python program to return multiple values from a method using class 
class tmp: 
  def __init__(self): 
    self.str = "AccuCloud"
    self.val = 20
# This function returns an object of tmp 
def func(): 
  return tmp()
# creating object of the func() 
a = func() 
print(a.str) 
print(a.val)

Output:

You can also return value using a tuple. A Tuple consists of items separated by commas and can be created both with or without parentheses. Tuples, once created, cannot be changed (they are immutable).

Example:

# A Python program to return multiple values from a method using tuple 
def tmp(): 
  stri = "Accu Cloud"
  a = 20
  return str, a; # Returning tuple
# Driver code to test above method 
stri, x = tmp() 
print(stri) 
print(x)

Output:

You can also return value using the list. A list, formed with square brackets, resembles an array and holds various items. Unlike arrays, lists can accommodate items of diverse types. They differ from tuples in that they can be mutable after creation.

Save $100 in the next
5:00 minutes?

Register Here

Example:

# A Python program to return multiple values from a method using list
def tmp():
stri = "Accu Cloud"
a = 10
return [stri, a];
list1 = tmp()
print(list1)

Output:

You can also return the dictionary. A Dictionary shares similarities with hashes or maps found in other programming languages.

Example:

# A Python program to return multiple values from a method using dictionary
def tmp(): 
  a = dict(); 
  a['stri'] = "Accu Cloud"
  a['z'] = 10
  return a
  a = tmp() 
print(a)

Output:

Here I have given you an example of which functions are like objects, so we can send back a function from another function. This works because, in Python, functions are treated as super flexible objects.

Example:

def fun1(a):
  def adder(b):
    return a + b
  return adder
adding = fun1(15)
print("The result is", adding(20))
def tmp(x):
  return x * 10
def my_func1():
  return tmp
res = my_func1()
print("Result is:", res(20))

Output:

Conclusion:

The return statement in Python helps us send back values from functions to the place they were called. It’s like handing over a package of information before the function finishes its job.