Professional Documents
Culture Documents
Functions
INSY5339
def myprint():
Stored Statements
def myprint():
print('Hello')
print(’Hello’)
print(’World')
print(‘World’)
myprint() myprint()
print(’Hi')
myprint()
Print(‘Finished’)
print(‘Hi’)
• Defined by us
• Defined by someone else
Functions defined by us
• 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.')
Using a function
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')
x = x + 2
print(x)
Function 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
argument
Function Parameters
• A parameter is a >>> def greet(lang):
... if lang == 'es':
variable which we use ... print('Hola')
in the function ...
...
elif lang == 'fr':
print('Bonjour')
definition. ... else:
... print('Hello')
• It is a “handle” that ...
allows the code in the >>> greet('en')
Hello
function to access the >>> greet('es')
arguments for a Hola
>>> greet('fr')
particular function Bonjour
invocation. >>>
Return Values
• Often a function will take
>>> def greet(lang):
its arguments, do some ... if lang == 'es':
computation, and return ... return 'Hola'
a value ... elif lang == 'fr':
• The return keyword is ... return 'Bonjour'
used for this. ... else:
... return 'Hello'
• A “fruitful” function is one ...
that produces a result (or >>> print(greet('en'),'Glenn')
Hello Glenn
return value) >>> print(greet('es'),'Sally')
• The return statement Hola Sally
ends the function >>> print(greet('fr'),'Michael')
execution and “sends Bonjour Michael
>>>
back” the result of the
function
Arguments, Parameters & Results
def max(inp):
blah
blah
'Hello world' 'w'
for x in inp:
blah
blah
Argument return 'w' Result
Optional Parameters
def multiply(x, y, z=1, a=1):
return(x*y*z*a)
print(multiply(2,3))
print(multiply(2,3,2))
print(multiply(2))
compute_consumption(20,25,80)
Functions written by others
import math
print(math.sqrt(x))
Common Modules: re