1 of 28

Functions: return

Andrea Wu

2 of 28

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

3 of 28

Review: Defining and Returning

def is_even(number):

if number % 2 == 0:

return True

else:

return False

is_even(5)

number = 5

4 of 28

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)

5 of 28

Calling Functions

float('2.25')

Function name

Argument

Parentheses

6 of 28

Return Value

float('2.25') returns 2.25

What to do with the return value?

print(float('2.25'))

7 of 28

Store into variable

float('2.25')

return value gets stored

Returns 2.25

print(num)

num =

8 of 28

Calling Functions

input('Name?')

Function name

Argument

Parentheses

9 of 28

Return Value

input('Name?') returns whatever the user input - 'Andrea'

What to do with the return value?

print(input('Name?'))

10 of 28

Store into variable

input('Name?')

return value gets stored

Returns 'Andrea'

print(choice)

choice =

11 of 28

EdStem: call, print, store

12 of 28

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

13 of 28

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?

14 of 28

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?

15 of 28

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

16 of 28

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

17 of 28

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

18 of 28

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

19 of 28

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

20 of 28

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

21 of 28

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

22 of 28

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

23 of 28

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

24 of 28

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

25 of 28

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

26 of 28

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

27 of 28

EdStem: print, return

28 of 28

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