Functions Slides
Functions Slides
Sarah Holderness
PLURALSIGHT AUTHOR
@dr_holderness
Functions We’ve Used
Functions are like mini-programs that complete a specific task.
We might not know how these work, but they give the expected results.
print('Hello World')
print('Hello World')
print('Hello World')
amount = int(10.6)
print('Hello World')
amount = int(10.6)
roll = random.randint(1,6)
def greeting(name):
The function body is
print('Hello', name)
indented below the definition.
Defining a Function
greetings.py
def greeting(name):
print('Hello', name) The function definition
greetings.py
def greeting(name):
The functions need to
print('Hello', name)
be defined first…
# Main program
input_name = input('Enter your name:\n')
greetings.py
# Main program
input_name = input('Enter your name:\n') The 1st line of code that
isn’t in a function definition
greeting(input_name) is where the program starts.
Flow Through a Program
greetings.py
# Main program
input_name = input('Enter your name:\n')
greetings.py
# Main program
input_name = input('Enter your name:\n')
greeting(input_name)
Flow Through a Program
greetings.py
greeting(input_name)
Flow Through a Program
greetings.py
greeting(input_name)
End of the program
Scope
A variable created inside a function can only be used inside that function.
This is called local scope.
greetings.py
# Main program
input_name = input('Enter your name:\n')
greeting(input_name)
print('Thanks', name) The variable name doesn’t exist here, outside
of the function, so this would give us an error.
Scope
A variable created in the main body of the program is a global variable
and has global scope. That means it can be used anywhere.
greetings.py
def greeting():
The variable name is global so we can
print('Hello', name)
reference it inside this function.
# Main program
name = input('Enter your name:\n')
The variable name is global.
greeting() We don’t need a parameter for greeting()
since it can reference the global variable name.
Global Scope
greetings.py
greetings.py
def greeting():
print('Hello', name)
The variable name is global.
# Main program
name = input('Enter your name:\n')
greeting()
name2 = input('Enter another name:\n')
Now how do we use the greeting()
function with name2?
name = name2
greeting() We could save name2 to the name variable. But then the
value for name is gone… Let’s try local scope again.
Local Scope
greetings.py
def greeting(name):
Now we can use the greeting() function
print('Hello', name)
with any passed in value for name.
# Main program
name1 = input('Enter your name:\n')
We have two different name values and
greeting(name1)
name2 = input('Enter another name:\n') we can use the greeting() function
for both of them.
greeting(name2)
Local Scope
greetings.py
def Function 2
Keyword name parameters
def addition( a, b ):
return a + b
return
Keyword
addition.py
# Main program
num1 = float(input('Enter your 1st number:\n')) The main program starts
num2 = float(input('Enter your 2nd number:\n')) running here.
# Calling our function
result = addition(num1, num2)
print('The result is', result)
Defining Our Function
addition.py
addition.py
addition.py
def main():
Now all of the program
num1 = float(input('Enter your 1st number:\n')) code is contained inside
num2 = float(input('Enter your 2nd number:\n')) this main() function.
main() We still need to call main() after the functions are declared.
Reasons to Create a Function