Functions: return
Andrea Wu
Review: Functions
What are they?
Block of reusable code with a name
Call function to run it
Pass arguments to function so function can do its job
Define function using def to give a name to block of code
Return a value from function
Why are they important?
Reuse code without having to copy and paste
Review: Defining and Returning
def is_even(number):
if number % 2 == 0:
return True
else:
return False
is_even(5)
number = 5
Return Values
def is_even(number):
if number % 2 == 0:
return True
else:
return False
What does returning False mean or do?
can print() it out:
print(is_even(5))
False
can store into variable
odd_or_even = is_even(5)
print(odd_or_even)
Calling Functions
float('2.25')
Function name
Argument
Parentheses
Return Value
float('2.25') returns 2.25
What to do with the return value?
print(float('2.25'))
Store into variable
float('2.25')
return value gets stored
Returns 2.25
print(num)
num =
Calling Functions
input('Name?')
Function name
Argument
Parentheses
Return Value
input('Name?') returns whatever the user input - 'Andrea'
What to do with the return value?
print(input('Name?'))
Store into variable
input('Name?')
return value gets stored
Returns 'Andrea'
print(choice)
choice =
EdStem: call, print, store
Function: Absolute Value
if num < 0:
answer = num * -1
else:
answer = num
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
function name
Passing arguments
result = abs(-8)
other_result = abs(2)
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
num = -8
answer = 8
result = 8
is num < 0?
Passing arguments
result = abs(-8)
other_result = abs(2)
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
num = 2
answer = 2
other_result = 2
is num < 0?
Arguments vs Parameters
result = abs(-8)
other_result = abs(2)
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
argument
parameter
Argument vs Parameter
argument: value provided as input to the function in function call
result = abs(-8)
other_result = abs(2)
def abs(num):
# code
argument
parameter
parameter: named variable representing input in the function definition
print() vs return
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
abs(-8) returns 8
print(abs(-8))
8
absolute_value = abs(-8)
print(absolute_value)
8
print() vs return
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
abs(-8) returns 8
print(abs(-8))
def abs(num):
if num < 0:
print(num * -1)
else:
print(num)
abs(-8)
print(abs(-8))
absolute_value = abs(-8)
print(absolute_value)
8
None
absolute_value = abs(-8)
print(absolute_value)
8
None
None
def abs(num):
if num < 0:
print(num * -1)
else:
print(num)
abs(-8)
print(abs(-8))
None: no value
When a function does NOT have a return statement, Python will return None
8
None
absolute_value = abs(-8)
print(absolute_value)
8
None
print() vs return
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
abs(-8)
def abs(num):
if num < 0:
print(num * -1)
else:
print(num)
answer = 8
print() vs return
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
abs(-8)
def abs(num):
if num < 0:
print(num * -1)
else:
print(num)
8
None
8
print(abs(-8))
answer = 8
print() vs return
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
abs(-8)
def abs(num):
if num < 0:
print(num * -1)
else:
print(num)
absolute_value = abs(-8)
8
None
8
8
print(abs(-8))
answer = 8
absolute_value = 8
absolute_value = None
print() vs return
def abs(num):
if num < 0:
answer = num * -1
else:
answer = num
return answer
abs(-8)
def abs(num):
if num < 0:
print(num * -1)
else:
print(num)
absolute_value = abs(-8)
8
None
8
None
8
8
print(abs(-8))
print(absolute_value)
answer = 8
absolute_value = 8
absolute_value = None
print != return
return: gives back (returns) value to caller of function
print: shows output to the user by printing it out
A function can both print() AND return
def abs(num):
if num < 0:
print('is negative')
answer = num * -1
else:
print('is positive or 0')
answer = num
return answer
abs(-8)
is negative
print != return
return: gives back (returns) value to caller of function
print: shows output to the user by printing it out
A function can both print() AND return
def abs(num):
if num < 0:
print('is negative')
answer = num * -1
else:
print('is positive or 0')
answer = num
return answer
abs(-8)
8
is negative
absolute_value = abs(-8)
print(abs(-8))
returns 8
is negative
8
is negative
print(absolute_value)
8
Example: input()
answer = input(“How are you today?”)
What gets printed is “How are you today?”
What gets returned from input() is whatever the user types in
EdStem: print, return
Review: Functions - return
What to do with return value
Print the return value
Store into a variable (can print out the variable value)
print() vs return
Not the same!
return: gives back (returns) value to caller of function
print: shows output to the user by printing it out
None
Represents no value
Gets returned if function has no return statement