Output: return statement

def cumulative_sum(n):
  total = 0
  for i in range(1,n+1):
    total += i
  return total

print("Cumulative sum from 1 to 4 is", cumulative_sum(4))
Cumulative sum from 1 to 4 is 10

return always exits the function!

def compute_it(x, y, z):
    print("Calling compute_it")
    w = x + y / z
    return w
    print("Done!") # this will never be executed
    
y = compute_it(1,2,3)
Calling compute_it

What if I don’t use return?

  • If a function has no return statement, Python returns from the function after the last statement is executed
  • The output of a function without return is the null object, which in Python is called None. For example:
def hello():
  print("Hello World!")

x = hello() # assigning the output of function hello() to x
print(x)
x = print("Hello") # assigning the output of the built-in function print() to x
print(x)
Hello World!
None
Hello
None

Returning multiple values

  • It is possible to return multiple values as a tuple:
def cumulatives(n):
  c_sum = 0
  c_prod = 1
  for i in range(1,n+1):
    c_sum += i
    c_prod *= i
  return c_sum, c_prod

a, b = cumulatives(4)
print("Cumulative sum is", a, "and cumulative product is", b)
Cumulative sum is 10 and cumulative product is 24