6-Python Functions in Simple
6-Python Functions in Simple
CS106AP Lecture 6
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!
Object-Oriented
Everyday Python
Programming
Object-Oriented
Everyday Python
Programming
5. What’s next?
Who am I?
Sonja
Johnson-Yu
Sonja
Johnson-Yu
Sonja
Johnson-Yu
Sonja
Johnson-Yu
Review
Variables
What is a variable?
A variable is a container for storing a data value.
5
num_flowers = 5
num_flowers
variable’s
name
What is a variable?
A variable is a container for storing a data value.
5
num_flowers = 5
num_flowers
variable’s
value
Terminology summary
+ Addition
- Subtraction
Arithmetic operators
Operator Precedence
* Multiplication
() 1
/ Division
*, /, //, % 2
// Integer division
+, - 3
% Modulus (remainder)
+ Addition
Integer division takes the largest integer that is
- Subtraction
equal to or smaller than the quotient
Integer Division Practice!
Number of iterations
For loop with range
for i in range(3):
do_something()
Built-in function
Range
range(3) -> iterates through 0,1,2
Range
range(3) -> iterates through 0,1,2
range(0, 3) -> iterates through 0,1,2
Range
range(3) -> iterates through 0,1,2
range(0, 3) -> iterates through 0,1,2
range(4, 7) -> iterates through 4,5,6
Range
for i in range(end_index):
# assumes 0 is the start index
Range
for i in range(end_index):
# assumes 0 is the start index
_ ri g ht()
tu r n
math.sqrt(4)
average(x, y) r e ()
t u
m pera
d i c t_te
pre
Karel Functions
def turn_right():
turn_left()
turn_left()
turn_left()
Karel Functions
def move_x_times():
# ????
Karel Functions
def move_x_times():
# ????
toaster()
toaster(bread)
bread
toaster(bread)
bread toast
toaster(bagel)
bagel
toaster(bagel)
bagel
You don’t need a different toaster for toasting bagels! Use the same one.
Slide adapted from Chris Piech
Function Analogy
toaster(bagel)
bagel toasted
bagel
output expected
Anatomy of a Function
def function_name(param1, param2):
result = # do something
return result
return value
Think/Pair/Share:
Find the function definition, function
name, parameter(s), and return value.
Anatomy of a Function Think/Pair/Share:
return value
Anatomy of a Function
def main():
mid = average(10.6, 7.2)
Definition
print(mid)
Return value
Value that a function
def average(a, b): hands back to the
sum = a + b
return sum / 2 “calling” function
return value
Anatomy of a Function
def main():
mid = average(10.6, 7.2)
Definition
print(mid)
Return value
Value that a function
def average(a, b): hands back to the
sum = a + b
return sum / 2 “calling” function
caller
def average(a, b): (calling function)
sum = a + b
return sum / 2
Anatomy of a Function
def main():
mid = average(10.6, 7.2)
print(mid)
caller
def average(a, b): (calling function)
sum = a + b
return sum / 2
callee
(called function)
Anatomy of a Function
def main():
mid = average(10.6, 7.2)
print(mid)
function “call”
def average(a, b):
sum = a + b
return sum / 2
Anatomy of a Function
def main():
mid = average(10.6, 7.2)
print(mid)
arguments
def average(a, b):
sum = a + b
return sum / 2
Anatomy of a Function
def main():
mid = average(10.6, 7.2)
print(mid)
arguments
def average(a, b):
sum = a + b
return sum / 2 What’s the difference between
arguments and parameters?
Anatomy of a Function
def main():
mid = average(10.6, 7.2)
print(mid)
2.0
Function
Recall from last lecture:
>>> math.sqrt(4)
2.0
Argument
Recall from last lecture:
>>> math.sqrt(4)
2.0
Return value
Anatomy of a Function
4 math.sqrt(4) 2.0
def function_name():
x = 2
y = 3 this is the scope
where x and y “live”
Variable Scope
def main():
function_name()
print(y)
def function_name():
x = 2
y = 3
Variable Scope
def main():
function_name()
print(y)
def function_name():
x = 2
y = 3
Variable Scope
def main():
function_name()
print(y)
x
2
def function_name():
x = 2
y = 3
Variable Scope
def main():
function_name()
print(y)
x
2
def function_name():
x = 2
y = 3 y
3
Variable Scope
def main():
function_name()
print(y)
def function_name():
x = 2
y = 3
Variable Scope
def main():
function_name()
print(y)
NameError
def function_name():
x = 2
y = 3
Variable Scope
def main():
function_name()
print(y)
y is now out of scope!
def function_name():
x = 2
y = 3
Variable Scope
def main():
function_name()
print(y)
y is now out of scope!
def function_name():
x = 2
y = 3
Once a function finishes executing, the
variables declared inside of it are no
longer accessible!
Unless...
def main():
y = function_name()
print(y)
def function_name():
x = 2
y = 3
return y
Unless...
def main():
y = function_name()
print(y)
if we return y, we
def function_name(): can use it in main()
x = 2
y = 3
return y
Let’s put it all together!
Receipt program
● What subtasks can we break this program into?
Receipt program
● What subtasks can we break this program into?
○ calculating tax
○ calculating the tip
○ aggregating tax and tip
[demo]
How do we translate what we know
from Karel into regular Python code?
Today’s How can we make our code more
● Interactive programs
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!
Python Strings and
Functions the Console
Object-Oriented
Everyday Python
Programming