1 of 15

Computer Science in Algebra

2 of 15

Contracts

Understanding what functions should do and how to make them do it

3 of 15

Vocab

  • Contract - a statement of the name, domain, and range of a function
  • Domain - the type of a data that a function expects
  • Range - the type of data that a function produces

4 of 15

What's in a Function?

  • What does the '+' function do?
    • What does it take as input?
    • What does it return as output?
  • How about the 'triangle' function?
  • What do these different functions have in common?
  • Let's look at a simple way to describe any function

5 of 15

Signing the Contract

  • What is a Contract?
    • A formal agreement
    • A description of expected behavior
  • Why do Contracts tell us?
    • What a function should do
    • What inputs a function needs
    • What a function returns

6 of 15

The 3 Parts of a Contract

  • Name
    • A descriptive name to tell us what a function does
  • Domain
    • The data types that a function takes as input
  • Range
    • The data type that a function will return

We write contracts in the form:

Name: Domain -> Range

7 of 15

Contract Examples

+: Number Number -> Number

  • What is this function's Name?
  • What is this function's Domain?
  • What is this function's Range?

8 of 15

Contract Examples

triangle: Number String String -> Image

  • What is this function's Name?
  • What is this function's Domain?
  • What is this function's Range?

9 of 15

Contract Examples

rotate: Number Image -> Image

  • What is this function's Name?
  • What is this function's Domain?
  • What is this function's Range?

10 of 15

Writing Contracts

  • We will write a contract for every function we use
    • This will be helpful reference when we write code
    • Keep this reference with you at all times
  • Start your contract reference by writing contracts for:
    • +
    • -
    • *
    • /

11 of 15

Contracts from Blocks

  • Evaluation Blocks give us some hints for contracts
    • The Name of each function is at the top
    • There will be a slot for each Domain element
    • The color of each slot tells you Domain type
    • The color of the whole block tells you Range
    • Color codes: Number String Image .

12 of 15

Writing Contracts

  • What is the Name of this function?
  • What is the Domain of this function?
  • What is the Range of this function?
  • Add this function's contract to your reference

13 of 15

Writing Contracts

  • What is the Name of this function?
  • What is the Domain of this function?
  • What is the Range of this function?
  • Add this function's contract to your reference

14 of 15

Writing Contracts

  • What is the Name of this function?
  • What is the Domain of this function?
  • What is the Range of this function?
  • Add this function's contract to your reference

15 of 15

Keep up your Contracts

As you continue programming, make sure you write a contract for every new function you encounter or write!