return values functions with parameters andsungeui/cs101/codes/lecture4/lecture4_yo… · cs101 de...
TRANSCRIPT
![Page 1: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/1.jpg)
CS101
CS101, Spring 2015
Functions with Parameters and
Return Values
Lecture #4
![Page 2: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/2.jpg)
CS101 Roadmap
Last week we covered• Objects and Types• Variables• Methods• Tuples
![Page 3: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/3.jpg)
CS101 Roadmap
Last week we covered• Objects and Types• Variables• Methods• Tuples
This week we will learn• Functions with parameters and return values
![Page 4: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/4.jpg)
CS101 Functions
The name function comes from mathematics. A function is amapping from one set to another set:
f : R→ R
x 7→ π × x
180.0
![Page 5: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/5.jpg)
CS101 Functions
The name function comes from mathematics. A function is amapping from one set to another set:
f : R→ R
x 7→ π × x
180.0
Here, x is the argument of the function, f(x) is the result ofthe function.
![Page 6: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/6.jpg)
CS101 Functions
In Python, functions also take arguments and return a result:
def to_radians(deg):
return (deg / 180.0) * math.pi
The name function comes from mathematics. A function is amapping from one set to another set:
f : R→ R
x 7→ π × x
180.0
Here, x is the argument of the function, f(x) is the result ofthe function.
![Page 7: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/7.jpg)
CS101 Functions
In Python, functions also take arguments and return a result:
def to_radians(deg):
return (deg / 180.0) * math.pi
The name function comes from mathematics. A function is amapping from one set to another set:
f : R→ R
x 7→ π × x
180.0
Here, x is the argument of the function, f(x) is the result ofthe function.
>>> a = to_radians(90)
>>> print a
1.5707963267948966
![Page 8: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/8.jpg)
CS101 Useful functions
Python comes with many built-in functions.
![Page 9: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/9.jpg)
CS101 Useful functions
Python comes with many built-in functions.
Type conversion functions convert from one type to anothertype:
>>> int("32")
32
>>> int(17.3)
17
>>> float(17)
17.0
>>> float("3.1415")
3.1415
>>> str(17) + " " + str(3.1415)
’17 3.1415’
>>> complex(17)
(17 + 0j)
![Page 10: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/10.jpg)
CS101 Math functions
To use math functions, we need to tell Python that we want touse the math module:
import math
degrees = 45
radians = degrees / 360.0 * 2 * math.pi
print math.sin(radians)
print math.sqrt(2) / 2
![Page 11: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/11.jpg)
CS101 Math functions
To use math functions, we need to tell Python that we want touse the math module:
import math
degrees = 45
radians = degrees / 360.0 * 2 * math.pi
print math.sin(radians)
print math.sqrt(2) / 2
When using math functions often, we can use shorter names:
import math
sin = math.sin
pi = math.pi
radians = degrees / 360.0 * 2 * pi
print sin(radians)
![Page 12: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/12.jpg)
CS101 Defining functions with parameters
The function definition uses names for the arguments of thefunction. These names are called parameters:
def compute_interest(amount, rate, years):
![Page 13: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/13.jpg)
CS101 Defining functions with parameters
The function definition uses names for the arguments of thefunction. These names are called parameters:
def compute_interest(amount, rate, years):
Inside the function, the parameter is just a name:
value = amount * (1 + rate/100.0) ** years
![Page 14: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/14.jpg)
CS101 Defining functions with parameters
The function definition uses names for the arguments of thefunction. These names are called parameters:
def compute_interest(amount, rate, years):
Inside the function, the parameter is just a name:
value = amount * (1 + rate/100.0) ** years
When we have computed the result of the function, we returnit from the function. The function ends at this point, and theresult object is given back:
return value
![Page 15: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/15.jpg)
CS101 Defining functions with parameters
The function definition uses names for the arguments of thefunction. These names are called parameters:
def compute_interest(amount, rate, years):
Inside the function, the parameter is just a name:
value = amount * (1 + rate/100.0) ** years
When we have computed the result of the function, we returnit from the function. The function ends at this point, and theresult object is given back:
return value
We can now call the function with different argument values:>>> s1 = compute_interest(200, 7, 1)
>>> s2 = compute_interest(500, 1, 20)
![Page 16: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/16.jpg)
CS101 Converting to black-and-white
What is the light intensity (luma) of pixel (r,g,b)?
![Page 17: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/17.jpg)
CS101 Converting to black-and-white
What is the light intensity (luma) of pixel (r,g,b)?
(255, 0, 0) (0, 255, 0) (0, 0, 255)
![Page 18: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/18.jpg)
CS101 Converting to black-and-white
What is the light intensity (luma) of pixel (r,g,b)?
(255, 0, 0) (0, 255, 0) (0, 0, 255)
A good formula is:
def luma(p):
r, g, b = p
return int(0.213 * r + 0.715 * g + 0.072 * b)
![Page 19: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/19.jpg)
CS101 More than one return in a function
Compute the absolute value (like builtin function abs):
def absolute(x):
if x < 0:
return -x
else:
return x
![Page 20: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/20.jpg)
CS101 More than one return in a function
Compute the absolute value (like builtin function abs):
def absolute(x):
if x < 0:
return -x
else:
return x
The same function can be written like this:
def absolute(x):
if x < 0:
return -x
return x
![Page 21: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/21.jpg)
CS101 More than one return in a function
Compute the absolute value (like builtin function abs):
def absolute(x):
if x < 0:
return -x
else:
return x
The same function can be written like this:
def absolute(x):
if x < 0:
return -x
return x
But not like this:
def absolute(x):
if x < 0:
return -x
if x > 0:
return x
![Page 22: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/22.jpg)
CS101 Returning a boolean
A function that tests a condition and returns either True orFalse is often called a predicate:
# is integer a divisible by b?
def is_divisible(a, b):
if a % b == 0:
return True
else:
return False
![Page 23: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/23.jpg)
CS101 Returning a boolean
A function that tests a condition and returns either True orFalse is often called a predicate:
# is integer a divisible by b?
def is_divisible(a, b):
if a % b == 0:
return True
else:
return False
A predicate (function) can be used directly in an if or whilestatement:
if is_divisible(x, y):
print ’x is divisible by y’
![Page 24: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/24.jpg)
CS101 Returning a boolean
A function that tests a condition and returns either True orFalse is often called a predicate:
# is integer a divisible by b?
def is_divisible(a, b):
if a % b == 0:
return True
else:
return False
A predicate (function) can be used directly in an if or whilestatement:
if is_divisible(x, y):
print ’x is divisible by y’
Easier:def is_divisible(a, b):
return a % b == 0
![Page 25: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/25.jpg)
CS101 Functions without results
We have seen many functions that do not use return:
def turn_right():
for i in range(3):
hubo.turn_left()
![Page 26: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/26.jpg)
CS101 Functions without results
We have seen many functions that do not use return:
def turn_right():
for i in range(3):
hubo.turn_left()
In fact, a function that does not call return automaticallyreturns None:
>>> s = turn_right()
>>> print s
None
![Page 27: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/27.jpg)
CS101 Calling functions
When a function is called, the arguments of the function callare assigned to the parameters:
def print_twice(text):
print text
print text
Parameter
![Page 28: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/28.jpg)
CS101 Calling functions
When a function is called, the arguments of the function callare assigned to the parameters:
def print_twice(text):
print text
print text
Parameter
The number of arguments in the function call must be thesame as the number of parameters.
>>> print_twice("I love CS101")
I love CS101
I love CS101
>>> print_twice(math.pi)
3.14159265359
3.14159265359
![Page 29: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/29.jpg)
CS101 Hubo’s family
We can now write a turn_right function that will work forany robot, not just for Hubo:
def turn_right(robot):
for i in range(3):
robot.turn_left()
ami = Robot("yellow")
hubo = Robot("blue")
turn_right(ami)
turn_right(hubo)
![Page 30: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/30.jpg)
CS101 Hubo’s family
We can now write a turn_right function that will work forany robot, not just for Hubo:
def turn_right(robot):
for i in range(3):
robot.turn_left()
ami = Robot("yellow")
hubo = Robot("blue")
turn_right(ami)
turn_right(hubo)
Remember: A parameter is a name for an object. The namecan only be used inside the function.
![Page 31: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/31.jpg)
CS101 Harvesting again
![Page 32: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/32.jpg)
CS101 Harvesting again
stairs(hubo, 5)
![Page 33: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/33.jpg)
CS101 Harvesting again
stairs(hubo, 5)
stairs(hubo, 3)
![Page 34: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/34.jpg)
CS101 Harvesting again
stairs(hubo, 5)
stairs(hubo, 3)
stairs(hubo, 1)
![Page 35: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/35.jpg)
CS101 Harvesting again
stairs(hubo, 5)
stairs(hubo, 5)stairs(hubo, 5)
stairs(hubo, 5)
![Page 36: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/36.jpg)
CS101
def stairs(robot, n):
for i in range(n):
robot.pick_beeper()
robot.move()
turn_right(robot)
robot.move()
robot.turn_left()
def diamond(robot, n):
for i in range(4):
stairs(robot, n)
robot.turn_left()
def harvest_all(robot):
for i in range(3):
n = 5 - 2 * i
diamond(robot, n)
robot.move()
robot.move()
![Page 37: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/37.jpg)
CS101 Converting to black and white, again
white = (255, 255, 255)
black = (0, 0, 0)
def blackwhite(img, threshold):
w, h = img.size()
for y in range(h):
for x in range(w):
v = luma(img.get(x, y))
if v > threshold:
img.set(x, y, white)
else:
img.set(x, y, black)
pict = load_picture("../photos/yuna1.jpg")
blackwhite(pict, 100)
pict.show()
![Page 38: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/38.jpg)
CS101 Returning more than one value
A function can only return one value.
![Page 39: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/39.jpg)
CS101 Returning more than one value
A function can only return one value.
But this value can be a tuple, and functions can returnarbitrarily many values by returning them as a tuple:
def student():
name = "Hong, Gildong"
id = 20101234
return name, id
![Page 40: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/40.jpg)
CS101 Returning more than one value
A function can only return one value.
But this value can be a tuple, and functions can returnarbitrarily many values by returning them as a tuple:
def student():
name = "Hong, Gildong"
id = 20101234
return name, id
Often function results are unpacked immediately:
name, id = student()
![Page 41: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/41.jpg)
CS101 Keyboard input
The raw_input function prints a message and waits for theuser to enter a string on the keyboard. When the user pressesthe Enter key, the whole string is returned:
name = raw_input("What is your name? ")
print "Welcome to CS101, " + name
![Page 42: Return Values Functions with Parameters andsungeui/cs101/Codes/Lecture4/lecture4_yo… · CS101 De ning functions with parameters The function de nition usesnamesfor the arguments](https://reader036.vdocuments.mx/reader036/viewer/2022071116/5ffd56367613475bb13716e8/html5/thumbnails/42.jpg)
CS101 Keyboard input
The raw_input function prints a message and waits for theuser to enter a string on the keyboard. When the user pressesthe Enter key, the whole string is returned:
name = raw_input("What is your name? ")
print "Welcome to CS101, " + name
If we need a number, we should convert the string:
raw_n = raw_input("Enter a positive integer> ")
n = int(raw_n)
for i in range(n):
print "*" * i