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
Python Functions
• There are two kinds of functions in Python.
- Built-in functions that are provided as part of Python - print(),
input(), type(), float(), int() ...
- Functions that we define ourselves and then use
• We treat function names as “new” reserved words
(i.e., we avoid them as variable names)
Functions of Our Own…
Building our Own Functions
• We create a new function using the def keyword followed by
optional parameters 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.')
print("I'm a lumberjack, and I'm okay.")
print_lyrics(): print('I sleep all night and I work all day.')
x = 5
print('Hello')
def print_lyrics():
print("I'm a lumberjack, and I'm okay.") Hello
print('I sleep all night and I work all day.') Yo
print('Yo') 7
x = x + 2
print(x)
Definitions and Uses
• Once we have defined a function, we can call (or invoke) it
as many times as we like
• This is the store and reuse pattern
x = 5
print('Hello')
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
print('Yo')
print_lyrics()
Hello
x = x + 2
print(x) Yo
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
7
Arguments
• 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
big = max('Hello world')
Argument
Parameters Parameter
>>> def greet(lang):
... if lang == 'es':
A parameter is a variable which ... print('Hola')
... elif lang == 'fr':
we use in the function definition. ... print('Bonjour')
It is a “handle” that allows the ... else:
... print('Hello')
code in the function to access ...
the arguments for a particular >>> greet('en')
Hello
function invocation. >>> greet('es')
Hola
>>> greet('fr')
Bonjour
>>> Argument
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.
def greet():
return "Hello" Hello Glenn
Hello Sally
print(greet(), "Glenn")
print(greet(), "Sally")
Return Value
>>> def greet(lang):
... if lang == 'es':
• A “fruitful” function is one ... return 'Hola'
that produces a result (or ...
...
elif lang == 'fr':
return 'Bonjour'
return value) ... else:
... return 'Hello'
• The return statement ends ...
>>> print(greet('en'),'Glenn')
the function execution and Hello Glenn
“sends back” the result of >>> print(greet('es'),'Sally')
Hola Sally
the function >>> print(greet('fr'),'Michael')
Bonjour Michael
>>>
Arguments, Parameters, and
Results
>>> big = max('Hello world') Parameter
>>> print(big)
w
def max(inp):
blah
'Hello world'
blah
for x in inp:
'w'
blah
blah
Argument return 'w'
Result
Multiple Parameters / Arguments
• We can define more than one
parameter in the function def addtwo(a, b):
definition added = a + b
return added
• We simply add more arguments
x = addtwo(3, 5)
when we call the function print(x)
• We match the number and order 8
of arguments and parameters