04 Python - Functions
04 Python - Functions
Chapter 4
reachus@cloudxlab.com
Function Definition
reachus@cloudxlab.com
Argument
Result
>>> big = max('Hello world')
>>> print (big)
w
>>> tiny = min('Hello world')
>>> print (tiny)
>>>
reachus@cloudxlab.com
Max Function
A function is some stored
>>> big = max('Hello world') code that we use. A
>>> print (big) function takes some
w
input and produces an
output.
def max(inp):
blah
'Hello world' blah 'w'
(a string) for x in y:
blah
(a string)
blah
reachus@cloudxlab.com
Building our Own Functions
• We create a new function using the def keyword followed by optional
parameters in parentheses
• 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.')
reachus@cloudxlab.com
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)
reachus@cloudxlab.com
Definitions and Uses
reachus@cloudxlab.com
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
reachus@cloudxlab.com
Arguments
• An argument is a value we pass into the function as its input when we
call the function
reachus@cloudxlab.com
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"
reachus@cloudxlab.com
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
>>>
reachus@cloudxlab.com
Arguments, Parameters, and Results
>>> big = max('Hello world')
>>> print (big)
Parameter
w
def max(inp):
blah
blah
'Hello world' for x in y:
'w'
blah
blah
Argument return 'w'
Result
reachus@cloudxlab.com
Multiple Parameters / Arguments
• We can define more than
one parameter in the
function definition
def addtwo(a, b):
added = a + b
• We simply add more return added
arguments when we call
the function x = addtwo(3, 5)
print (x)
• We match the number and 8
order of arguments and
parameters
reachus@cloudxlab.com
Void (non-fruitful) Functions
reachus@cloudxlab.com
To function or not to function...
• Organize your code into “paragraphs” - capture a complete thought
and “name it”
• Make a library of common stuff that you do over and over - perhaps
share this with your friends...
reachus@cloudxlab.com
Exercise
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
475 = 40 * 10 + 5 * 15
reachus@cloudxlab.com
Summary
• Functions • Arguments
• Built-In Functions • Results (fruitful functions)
> Type conversion (int, float) • Void (non-fruitful) functions
> String conversions • Why use functions?
• Parameters
reachus@cloudxlab.com
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.