Make delicious recipes!

Functions in Python

Function is a re-usable piece of code which can be called with arguments.

# define a function with name foo
# all lines indented with respect to next line form the body of this function
def foo (var1, var2):  
  if (var1 == var2):
    print ("var1 equals var2")
  elif (var1 < var2):
    print ("var1 < var2")
    print ("var1 > var2")

# function foo can be called as follows:
foo (3,4)

# output:
var1 < var2

# If function was called as:
foo (5,4)

# Then output would have been
var1 > var2

Note: The copy paste from the above may remove the indentation spaces due to which python can give indentation errors. If such a thing happens, then indentation spaces have to be put manually after copy-paste.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal