1 of 17

Piecewise Functions and Conditionals

@BootstrapWorld

2 of 17

Intro to Piecewise Functions

How would you explain a function to someone else? What are some rules that all functions follow?

3 of 17

Intro to Piecewise Functions

Today we are going to act out a special kind of function.

  • If your birthday is in the summer, make an O with your arms
  • If your birthday is in the fall, make an X with your arms or body.
  • If your birthday is in the winter, put your hand on your head.
  • If your birthday is in the spring, flap your arms like a chicken.

What is the input of the function we just acted out? The output? How do we know that you just acted out a function?

4 of 17

Intro to Piecewise Functions

Up until now, all of the functions we have seen can be described by a single rule.

In this activity your behavior followed a set of rules for which each input still had exactly one output.

This is called a piecewise function in math, and a conditional in programming. The world is full of piecewise functions and conditionals!

5 of 17

Intro to Piecewise Functions

Let's try acting out another set of rules.

  • Everyone wearing sneakers put your hands on your head.
  • Everyone wearing a t-shirt make a T with your arms.
  • Everyone wearing pants put your hands on your hips.
  • Everyone wearing a t-shirt make an O with your arms.

Why doesn't this set of directions represent a function?

6 of 17

Intro to Piecewise Functions

Work with your partner to think of real-world examples of piecewise functions.

Then, we'll share out what we came up with.

7 of 17

Conditionals in Programming

The rule for gt was to take a number and make a solid, green triangle of that size. The rule for bc was to take a number and make a solid, blue circle of that size.

What if we want to write functions that apply different rules, based on certain conditions?

8 of 17

Conditionals in Programming

  • Open the RED SHAPE
  • Complete Red Shape - Explore

9 of 17

Conditionals in Programming

What happened when you gave red-shape a shape that wasn't defined in the program?

What is the syntax for writing piecewise functions?

10 of 17

Extending the Design Recipe

Let's see how the Design Recipe could help us to write a piecewise function.

  • Turn to Word Problem: red-shape
  • How do the Contract and Purpose Statement compare to other Contracts we've seen?
  • How do the examples compare to other examples we've seen?

11 of 17

Extending the Design Recipe

  • Circle and label everything that is changeable on Word Problem: red-shape.
  • What changes? What did you label?

If there are more unique labels in the examples than there are things in the Domain, we're probably looking at a piecewise function.

Think back to our examples of piecewise functions (ticket sales, postage, cell-phone data plans, etc): knowing the input isn't enough - we also need to know the conditions!

12 of 17

Extending the Design Recipe

In this example, we have four totally different patterns:

  • sometimes we produce (circle 20 "solid" "red")
  • sometimes we produce (triangle 20 "solid" "red")
  • sometimes we produce (rectangle 20 20 "solid" "red")
  • sometimes we produce (star 20 "solid" "red")
  • sometimes we produce (text "Unknown shape name!" 20 "red")

13 of 17

Extending the Design Recipe

To define a piecewise function, each condition has both a result ("what should we do") and a question ("when should we do it?").

  • When do we make circles? When shape == "circle"
  • When do we make triangles? When shape == "triangle"
  • When do we make rectangles? When shape == "rectangle"
  • When do we make stars? When shape == "star"
  • When do we draw the "Unknown shape name" text? When shape is....anything else

14 of 17

Extending the Design Recipe

Adding the questions to our pattern gives us:

  • When shape == "circle"...we produce (circle 20 "solid" "red")
  • When shape == "triangle"...we produce (triangle 20 "solid" "red")
  • When shape == "rectangle"...we produce (rectangle 20 20 "solid" "red")
  • When shape == "star"...we produce (star 20 "solid" "red")
  • When shape is anything else...we produce (text "Unknown shape name!" 20 "red")

15 of 17

Extending the Design Recipe

This practically gives away the body of our function definition!

16 of 17

Extending the Design Recipe

  • How many examples are needed to fully test a piecewise function with four "pieces"?
  • What changes in a piecewise function?

17 of 17

Additional Practice