Defining Functions in Python

  • def keyword is used to define a function
  • all variables in a function are local unless defined as global with the keyword global
  • return statement returns the value form the function
  • default values can be defined in function parameters

Defining Python Functions Example 1

# return the maximum value
def maxx(m,n):
    if m > n:
        return m
    else: 
        return n

print maxx(12,45)

Defining Python Functions Example 2

# compute circumference
def circumference(r, pi = 3.14):
    return 2 * pi * r;

# function called with both parameters
print circumference(12,3.1)    # 74.4

# function called with one parameter, pi would assume default value
print circumference(10)          # 62.8