Parameters and Arguments


I just have a question, why in this example we type return

Call and Response

def square(n):
  """Returns the square of a number."""
  squared = n ** 2
  print "%d squared is %d." % (n, squared)
  return squared

and in this one we dont use it.

Parameters and Arguments

def power(base, exponent):  # Add your parameters here!
  result = base ** exponent
  print "%d to the power of %d is %d." % (base, exponent, result)

power(37, 4)  # Add your arguments here!


Because the second function prints the result immediately without returning a value. We use return when we need a value to work with after the function is complete. If we just want to see the results, no return is needed. It is up to the author.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.