Programming‎ > ‎Python‎ > ‎


Python Functions

• There are two kinds of functions in Python.
> Built-in functions that are provided as part of Python - raw_input(), type(), float(), int() ...
> Functions that we define ourselves and then use

• We treat the built-in function names as “new” reserved words
(i.e., we avoid them as variable names)

Function Definition

• In Python a function is some reusable code that takes arguments(s) as input, does some computation, and then returns a result or results
• We define a function using the def reserved word
• We call/invoke the function by using the function name, parentheses, and arguments in an expression

Building our Own Functions

• We create a new function using the def keyword followed by optionalparameters in parentheses
• We indent the body of the function
• This defines the function but does not execute the body of the function

def print_lyrics():
    print "I'm a lumberjack, and I'm okay."
    print 'I sleep all night and I work all day.'



• An argument is a value we pass into the function as its input when we call the function
• We use arguments so we can direct the function to do different kinds of work when we call it at different times
• We put the arguments in parentheses after the name of the function


A parameter is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation.

>>> def greet(lang):
... if lang == 'es':
... print 'Hola'
... elif lang == 'fr':
... print 'Bonjour'
... else:
... print 'Hello'
>>> greet('en')
>>> greet('es')
>>> greet('fr')

Return Values

Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this.

• The return statement ends the function execution and “sends back” the result of the function

def greet():
    return "Hello"

print greet(), "Glenn"
print greet(), "Sally"

Multiple Parameters/Arguments

• We can define more than one parameter in the function definition
• We simply add more arguments when we call the function
• We match the number and order of arguments and parameters

def addtwo(a, b):
    added = a + b
    return added

x = addtwo(3, 5)
print x

Optional Parameters

All the optional parameters have to be at the right most side and have the default value.
def addtwo(a, b=5):
    added = a + b
    return added

Using regular arguments
print(addtwo(1, 2))

Using optional argument

Using labelled arguments
print(addtwo(a=1, b=2))

Using labelled arguments

Using labelled arguments
<TypeError: addtwo() missing 1 required positional argument: 'a'>

Assign function to a variable

Function could be assigned to a variable and then variable is used as function reference and even passed to other function.

def addtwo(a, b):
    added = a + b
    return added

def funccall(y, a, b):
  return y(a,b)

z = addtwo
print(z(1, 2))

print(funccall(z, 3, 4))

To function or not to function

• Organize your code into “paragraphs” - capture a complete thought and “name it”
• Don’t repeat yourself - make it work once and then reuse it
• If something gets too long or complex, break it up into logical chunks and put those chunks in functions
• Make a library of common stuff that you do over and over - perhaps share this with your friends...