CSE 160
Functions
Hannah Cheung
Announcements
2
Math Functions
len, float, int, str, range
3
Python Functions
4
Using (Calling) Functions
len(“hello”)
len(“”)
math.sqrt(9)
math.sqrt(7)
range(1, 5)
range(8)
math.sin(0)
str(17)
random.random()
print()
5
Function call examples
import math
x = 8
y = 16
z = math.sqrt(16)
u = math.sqrt(y)
v = math.sqrt(8 + 8)
w = math.sqrt(x + x)
6
What are the
Function call examples
greeting = “hi”
name = “Hannah”
a = len(“hello”)
b = len(greeting)
c = len(“hello” + “Hannah”)
print(“hello”)
print()
print(len(greeting + name))
7
What are the
Function call examples
greeting = “hi”
name = “Hannah”
a = len(“hello”)
b = len(greeting)
c = len(“hello” + “Hannah”)
print(“hello”)
print()
print(len(greeting + name))
8
What are the
Some functions are like a machine
9
2x + 1
2x + 1
2x + 1
x
x
x
2
0
100
5
f(x)
1
f(x)
201
f(x)
Defining a function
def dbl_plus(x):
return 2 * x + 1
10
keyword that defines function
2x + 1
x
f(x)
Defining a function
def dbl_plus(x):
return 2 * x + 1
11
keyword that defines function
2x + 1
x
f(x)
name of function
Defining a function
def dbl_plus(x):
return 2 * x + 1
12
keyword that defines function
2x + 1
x
f(x)
name of function
input variable name or parameter
Defining a function
def dbl_plus(x):
return 2 * x + 1
13
keyword that defines function
2x + 1
x
f(x)
name of function
input variable name or parameter
keyword that specifies result
Defining a function
def dbl_plus(x):
return 2 * x + 1
14
keyword that defines function
2x + 1
x
f(x)
name of function
input variable name or parameter
keyword that specifies result
return expression (part of return statement)
Executing a function call
def square(x):
return x * x
square(3 + 4)
15
Function definition
Function call
Executing a function call
def square(x):
return x * x
square(3 + 4)
square(3 + 4)
square(7)
16
Function definition
Function call
Executing a function call
def square(x):
return x * x
square(3 + 4)
square(3 + 4)
square(7)
17
Function definition
Function call
x: 7
Executing a function call
def square(x):
return x * x
square(3 + 4)
square(3 + 4)
square(7)
18
Function definition
Function call
x: 7
return 7 * 7
Executing a function call
def square(x):
return x * x
square(3 + 4)
square(3 + 4)
square(7)
19
Function definition
Function call
x: 7
return 7 * 7
square(3 + 4) = 49
Function definitions and calls
def dbl_plus(x):
return 2 * x + 1
def instructor_name():
return “Hannah Cheung”
def calc_grade(points):
grade = points * 10
return grade
20
What are the
Function definitions and calls
def dbl_plus(x):
return 2 * x + 1
def instructor_name():
return “Hannah Cheung”
def calc_grade(points):
grade = points * 10
return grade
# main program
dbp3 = dbl_plus(3)
dbp4 = dbl_plus(4)
print(dbp3 + db4)
print(instructor_name())
my_grade = calc_grade(dbp3)
21
What are the
Function definitions and calls
def calc_grade(points):
grade = points * 10
return grade
def print_grade(points):
grade = points * 10
print(“Grade is:”, grade)
# main program
my_grade = calc_grade(10)
print_grade(10)
22
No return statement
Returns the value None
Executed for side effect
All in same file
Functions can call functions
def fahr_to_celsius(fahr):
return (fahr - 32) / 9.0 * 5
def celsius_to_fahr(celsius):
result = celsius / 5.0 * 9 + 32
return result
def print_fahr_to_celsius(fahr):
result = fahr_to_celsius(fahr)
print(result)
# main program
boiling = fahr_to_celsius(212)
cold = celsius_to_fahr(-30)
print(print_fahr_to_celsius(32))
23
No return statement
Returns the value None
Executed for side effect
All in same file
Warmup #1 - What does this print?
def celsius_to_fahr(celsius):
print(celsius / 5.0 * 9 + 32)
print(celsius_to_fahr(20))
24
Warmup #1 - What does this print?
def celsius_to_fahr(celsius):
print(celsius / 5.0 * 9 + 32)
return None
print(celsius_to_fahr(20))
25
Warmup #2 - What does this print?
def my_func(n):
total = 0
for i in range(n):
total = total + i
return total
print(my_func(4))
26
Warmup #3 - What does this print?
def c_to_f(c):
print(“c_to_f”)
return c / 5.0 * 9 + 32
def make_message(temp):
print(“make_message”)
return “The temperature is “ + str(temp)
for tempc in [-40, 0, 37]:
tempf = c_to_f(tempc)
message = make_message(tempf)
print(message)
27
Digression: Two types of output
28
Looking up variables
29
Looking up variables
x = 22
stored = 100
def lookup():
x = 42
return stored + x
val = lookup()
x = 5
stored = 200
val = lookup()
30
Looking up variables: masking
x = 22
stored = 100
def lookup():
x = 42
return stored + x
val = lookup()
x = 5
stored = 200
val = lookup()
x = 22
stored = 100
def lookup():
x = 42
stored = stored + x
return stored
val = lookup()
x = 5
stored = 200
val = lookup()
31
Temporary variables
def store_it(arg):
stored = arg
return stored
stored = 0
y = store_it(22)
print(y)
print(stored)
32
How many x variables?
def square(x):
return x * x
def abs(x):
if x < 0:
return -x
else:
return x
# main program
x = 42
sq3 = square(3)
print(x)
x = -22
result = abs(x)
print(result)
33
All in same file
Local and global scope
myvar = 1
def outer():
myvar = 1000
temp = inner()
return temp
def inner():
return myvar
print(outer())
34
Designing functions
35
Variables should represent one thing
# Legal, but confusing: don’t do this!
x = 3
…
x = “hello”
…
x = [3, 1, 4, 1, 5]
36
Each variable should contain values of only one type
Designing functions
# Main program
tempf = 32
print(“Temp in Fahr:”, tempf)
tempc = fahr_to_celsius(tempf)
print(“Temp in Celsius”, tempc)
37
Designing functions
# Main program
tempf = 32
print(“Temp in Fahr:”, tempf)
tempc = fahr_to_celsius(tempf)
print(“Temp in Celsius”, tempc)
def fahr_to_celsius(fahr):
“““
Input: Number of degrees Fahrenheit
Return: Number of degrees Celsius
”””
38
Designing functions
# Main program
tempf = 32
print(“Temp in Fahr:”, tempf)
tempc = fahr_to_celsius(tempf)
print(“Temp in Celsius”, tempc)
def fahr_to_celsius(fahr):
“““
Input: Number of degrees Fahrenheit
Return: Number of degrees Celsius
”””
assert fahr_to_celsius(32) == 0
assert fahr_to_celsius(212) == 100
assert fahr_to_celsius(98.6) == 37
assert fahr_to_celsius(-40) == -40
39
Designing functions
# Main program
tempf = 32
print(“Temp in Fahr:”, tempf)
tempc = fahr_to_celsius(tempf)
print(“Temp in Celsius”, tempc)
def fahr_to_celsius(fahr):
“““
Input: Number of degrees Fahrenheit
Return: Number of degrees Celsius
”””
return (fahr - 32) / 9.0 * 5
assert fahr_to_celsius(32) == 0
assert fahr_to_celsius(212) == 100
assert fahr_to_celsius(98.6) == 37
assert fahr_to_celsius(-40) == -40
40
Documentation
Functions are an abstraction
42
Types of documentation
def square(x):
“““
Returns the square of its argument x.
”””
# Uses “x * x” instead of “x ** 2”
return x * x
43
for users - string as first element of function body
Types of documentation
def square(x):
“““
Returns the square of its argument x.
”””
# Uses “x * x” instead of “x ** 2”
return x * x
44
for users - string as first element of function body
for programmers - text after #
Multi-line strings
“““
Hello
world
”””
45
Comments to not write
# increment value of x
x = x + 1
46
Where to write comments
# increment value of x
x = x + 1
x = x + 1 # increment value of x
47