Py4Inf 04 Functions Print
Py4Inf 04 Functions Print
def max(inp):
• Do you want to take control of error recovery? • If the code in the try fails - it jumps to the except section
Math Library
(in radians)
(in radians)
(in radians)
(returns radians)
• Python also includes common
>>> import math
(returns radians)
math functions (returns radians)
>>> print math.sqrt(25.0)
• You must add import math to 5.0
your program to access this
library of functions
Trigonometry pi
----
Math Function Summary
Review 45
cos
4
print “Hello”
• We create a new function using the def keyword followed by optional def print_lyrics():
parameters in parenthesis. print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day." Hello
• We indent the body of the function Yo
print “Yo” 7
• This defines the function but does not execute the body of the function
x=x+2
print x
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."
Definitions and Uses 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."
• Once we have defined a function, we can call (or invoke) it as many
print “Yo”
times as we like
print_lyrics() Hello
• This is the store and reuse pattern x=x+2
print x
Yo
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
7
x=5
print “Hello”
Flow of Execution
Arguments
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
• An argument is a value we pass into the function as its input when we
call the function
print "I sleep all night and I work all day."
• We use arguments so we can direct the function to do different kinds
print “Yo” of work when we call it at different times
print_lyrics() Hello
x=x+2 Yo • We put the arguments in parenthesis after the name of the function
print x I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
7 big = max('Hello world')
Argument
>>> def greet(lang): >>> def greet(lang):
Parameters ... if lang == 'es':
... return 'Hola' Return Value ... if lang == 'es':
... return 'Hola'
... elif lang == 'fr': ... elif lang == 'fr':
... return 'Bonjour' ... return 'Bonjour'
... else: ... else:
• A parameter is a variable ... return 'Hello' • A “fruitful” function is one ... return 'Hello'
which we use in the ... that produces a result (or ...
function definition that is a >>> print greet('en'),'Glenn' return value) >>> print greet('en'),'Glenn'
“handle” that allows the Hello Glenn Hello Glenn
code in the function to >>> print greet('es'),'Sally' • The return statement ends >>> print greet('es'),'Sally'
access the arguments for a Hola Sally the function execution and Hola Sally
particular function >>> print greet('fr'),'Michael' “sends back” the result of >>> print greet('fr'),'Michael'
invocation. Bonjour Michael the function Bonjour Michael
>>> >>>
• Don’t repeat yourself - make it work once and then reuse it Enter Hours: 45
Enter Rate: 10
• If something gets too long or complex, break up logical chunks and put Pay: 475.0
those chunks in functions
• Make a library of common stuff that you do over and over - perhaps 475 = 40 * 10 + 5 * 15
share this with your friends...
Summary
• Functions • Parameters
• Built-In Functions • Results (Fruitful functions)
• Type conversion (int, float) • Void (non-fruitful) functions
• Math functions (sin, sqrt) • Why use functions?
• Try / except (again)
• Arguments