More Function Writing
CS 110
Reminders and Announcements
Next Week
def name_of_function(parameters):
statement(s)
3
Keyword def marks the start of function header
A function name to uniquely identify it (snake case)
parameters (the way we pass data to a function)
colon (:) marks end of function header
function body has 1 or more statements, which have same indentation level (usually 4 spaces)
A function with inputs
# defining the function
def say_hello(name, time_of_day='morning'):
print('Hello there,' + name + '!')
print('How are you this ' + time_of_day + '?')
# calling (invoking) the function
say_hello("Connor")
say_hello("Connor", time_of_day='evening')
4
Function with 2 parameters
keyword parameter
No keyword argument (uses default)
Keyword argument overrides default
The rectangle function (different from Tutorial)
Inputs:
center_x (int) - center x-coordinate
center_y (int) - center y-coordinate
width (int) - how wide is the rectangle
height (int) - how tall is the rectangle
color (str, optional) - what color is the rectangle (defaults to "hotpink")
Output:
A rectangle on the screen!
5
The square function challenge
Inputs:
center_x (int) - center x-coordinate
center_y (int) - center y-coordinate
size (int) - length and width of the square
color (str, optional) - what color is the square (defaults to "hotpink")
Output:
A square on the screen!
6
The squareseye function Challenge
Inputs:
center_x (int) - center x-coordinate
center_y (int) - center y-coordinate
size (int) - size of inner-most square (size…size*1.5…size*2.0…size*2.5)
color_a (str, optional) - color of inner-most square (defaults to blue)
color_b (str, optional) - color of inner-most square (defaults to red)
Output:
A squareseye (4 concentric squares similar to the ones on the right hand of the screen)
7
The squareface function Challenge
Inputs:
center_x (int) - center x-coordinate
center_y (int) - center y-coordinate
width (int) - width of the face
eye_color (str, optional) - (defaults to blue)
face_color (str, optional) - (defaults to gold)
Output:
A squareface (similar to the one on the right hand of the screen)
8
The squareface function Challenge
Inputs:
center_x (int) - center x-coordinate
center_y (int) - center y-coordinate
width (int) - width of the face
eye_color (str, optional) - (defaults to blue)
face_color (str, optional) - (defaults to gold)
Output:
A squareface (similar to the one on the right hand of the screen)
Eyes should be 1/20 the width of the face; 1/10 the height of the face, offset by ⅕ the width of the face in both directions.
9