Lecture 7 Slides - Writing Functions Practice

1 of 9

More Function Writing

CS 110

2 of 9

Reminders and Announcements

  • Exercise 2 due this evening!
  • Don't forget about edSTEM! (But don't use it as a last-minute lifeline)
  • Lots of office hours today
  • ALL submissions have been graded. If you don’t have a grade for something you submitted or have a 0 and believe you turned something in, something has gone wrong.

Next Week

  • Monday - Reporters
  • Wednesday - (Pre-Recorded MQ 5) Sequences + Tutorial 3
  • Friday - Scope + Mini-Quiz 6
    • Exercise 3

3 of 9

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

4 of 9

# 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

5 of 9

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

6 of 9

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

7 of 9

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

8 of 9

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

9 of 9

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