function – What does return True/False actually do? (Python)

function – What does return True/False actually do? (Python)

Because most of the time youll want to do something with the result other than print it. By using a return value, the function does one thing: do the calculation, and the caller can do whatever it wants with it: print it, write it to a database, use it as part of some larger calculation, whatever. The idea is called composability.

Also your example doesnt currently make much sense as it will always return False after evaluating x > y but doing nothing with the result. Perhaps you meant something like:

def is_greater(x, y):
    if x > y:
       return True
    else:
       return False

Then is_greater is something that can easily be used in whatever context you want. You could do:

x = is_greater(a, b)
write_to_super_secret_database(x)

(In real life you probably wouldnt use a function to do something so trivial, but hopefully the example makes sense.)

Functions which return True / False are used in further statements like IF:

Like this:

def is_bigger(x,y):
  return x > y

if is_bigger(10,9):
  do_something
elif:
  print math dont work anymore

you should take a look at variables and control structures:
http://www.tutorialspoint.com/python/python_if_else.htm

function – What does return True/False actually do? (Python)

Leave a Reply

Your email address will not be published. Required fields are marked *