CS in Algebra: Contracts

Alphabets Sounds Video

share us on:

In this lesson, students learn about the concept of contracts in programming, which serve as instructional guides for functions by detailing their name, domain, and range. Using the triangle function as an example, the lesson emphasizes the importance of understanding the types of data a function expects and returns, allowing for greater flexibility in programming. Students are encouraged to document their own function contracts in a log, which will aid in their development of more complex programs.

CS in Algebra: Understanding Contracts

Have you ever used functions like addition, subtraction, multiplication, or division? These are all functions that take two numbers and give you back another number. But did you know there are also functions that work with different types of data? For example, functions like star, triangle, and circle take a number and two strings and return an image. This is where something called a “contract” becomes really useful.

What is a Contract?

A contract is like an instruction manual for a function. It helps us understand how a function works by breaking it down into three parts: the name, the domain, and the range. Let’s explore these parts using the triangle function as an example.

The Name

The name of the function is simply what we call it. In our example, the function is named triangle. This part is straightforward and helps us identify the function.

The Domain

The domain of a function lists the types of data the function expects. It’s important to note that the domain is not about the names of the inputs, like size or color, but rather the types of data, such as numbers or strings. For instance, the triangle function might expect a number and two strings as its inputs.

The Range

The range tells us what type of data the function will return. For the triangle function, the range is an image. By using data types instead of specific values, contracts allow us to be more flexible. For example, a star function could take any number for its size, so its domain would specify that the first input is a number.

Why are Contracts Important?

Think of a programming language as a set of Lego pieces. Contracts are like the tabs and slots that show us how each piece can fit together. As you learn more about functions and start writing your own, documenting their contracts in a contract log will be very helpful. This log acts as a guidebook for your programming language, especially when you start creating more complex programs.

What’s Next?

In the next steps, you’ll have the opportunity to write different functions and create a contract for each one. Make sure to add these contracts to your log so you can refer back to them later. This practice will help you become more comfortable with writing and understanding functions.

  1. How do you think the concept of a contract in programming can be applied to other areas of your life or work?
  2. Reflect on a time when you used a function without understanding its contract. What challenges did you face, and how might understanding the contract have helped?
  3. In what ways do you think documenting contracts could improve your programming skills and project outcomes?
  4. Consider the analogy of programming languages as Lego pieces. How does this perspective change your approach to learning and using programming languages?
  5. How might understanding the domain and range of a function influence the way you design or debug a program?
  6. What are some potential consequences of not adhering to a function’s contract when writing code?
  7. How do you think the practice of creating a contract log can aid in collaborative programming projects?
  8. What insights did you gain about the importance of data types in programming from the article, and how might these insights affect your future coding practices?
  1. Create Your Own Function

    Imagine a function that takes a number and a string and returns a sentence. Write down the name, domain, and range for this function. Share your function with a classmate and see if they can understand it based on your contract.

  2. Function Contract Matching Game

    We’ll provide you with a list of function names, domains, and ranges. Your task is to match each function with its correct domain and range. Work in pairs to see who can complete the matches the fastest!

  3. Contract Log Creation

    Start a contract log in your notebook. Each time you learn a new function, write down its contract. This will be your personal guidebook as you continue to explore programming.

  4. Function Role-Playing

    In groups, choose a function and act out its process. One person can be the function name, others can represent the domain inputs, and another can be the range output. This activity will help you visualize how functions work.

  5. Design a Function Poster

    Create a poster that explains a function of your choice. Include its name, domain, and range, and illustrate how it works. Display your poster in the classroom to help others learn about different functions.

Here’s a sanitized version of the transcript:

You’ve already seen several functions that take in two numbers and return a number, such as addition, subtraction, multiplication, and division. The functions star, triangle, and circle all take in a number and two strings, returning an image. That’s where the contract comes in. A contract is a tool to help us document all of the functions in our language. It’s like an instruction manual for a specific function.

Let’s use the triangle function as an example. Each contract is made up of three parts: the name, the domain, and the range. The name should be pretty self-explanatory, and in this case, the name of our function is triangle. The domain of a function is a list of the types of data the function expects in the order it expects them. It’s important to understand that the domain is NOT the names of the inputs, like size, style, or color, but the data types they expect, like number and string.

The final element of our contract is the range, which tells us the data type that a function returns. The triangle function has a range of image. We use types instead of values when we write a contract because we want to be more general. A star could be any size, so the domain for the star specifies that the first input could be any number value.

If we think of a language as a collection of Lego pieces, the contracts are the tabs and slots that tell us how each piece can connect. As you encounter and eventually write more functions, you’ll want to document their contracts in the contract log. The log will serve as your manual for the language, which will be really helpful when you start to write more complex programs.

In the next stage, you’ll get a chance to write a number of different functions and then write a contract for each one. Make sure you also add these contracts to your log for reference later on.

This version maintains the original content while ensuring clarity and coherence.

FunctionsA block of code designed to perform a specific task in a program. – In our coding class, we learned how to create functions to make our programs more organized and efficient.

ContractA description of what a function does, including its inputs and outputs. – Before writing the code, the programmer wrote a contract to outline what the function should accomplish.

DomainThe set of all possible inputs for a function. – When designing a function, it’s important to consider the domain to ensure it can handle all expected inputs.

RangeThe set of all possible outputs that a function can produce. – The range of the function was limited to numbers between 1 and 100.

DataInformation processed or stored by a computer. – The program analyzed the data to find patterns and trends.

TriangleA shape with three sides and three angles, often used in computer graphics. – In the graphics program, we used triangles to create complex 3D models.

ImageA visual representation of something, often created or manipulated using computer software. – The image editing software allowed us to enhance and modify digital photos.

ProgrammingThe process of creating a set of instructions for a computer to follow. – We spent the afternoon programming a simple game using a new coding language.

InputsThe data or signals received by a computer or program to process. – The game required multiple inputs from the player to navigate through the levels.

LogA record of events or processes that occur in a computer system. – The system log helped us identify the error that caused the program to crash.

All Video Lessons

Login your account

Please login your account to get started.

Don't have an account?

Register your account

Please sign up your account to get started.

Already have an account?