Understanding Functions and Data Types in Programming
Exploring the concepts of functions, domains, and contracts in programming, as well as distinguishing between different data types such as numbers, strings, and images. Learn how to define functions, specify their input domains, and understand the output ranges they produce. Discover the importance of keeping track of functions and their usage in a programming language.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
DAY 3 OF BOOTSTRAP STRINGS AND IMAGES S I T I N YO U R PA I R S . G R A B YO U R C H R O M E B O O K S A N D L O G I N TO W E S C H E M E . O R G
STRINGS AND IMAGES One of these things is not like the other 12 -53 hello 4.9 A string is anything between quotation marks. What happens if you put quotes around multiple words? Around numbers? Try entering different strings into the Interactions window. 2
How would you convert this to code? star (star 50 solid red ) 50 solid red
A new function called star. (star has been defined previously in the computer program) The addition function + takes in two Numbers and produces a Number The function called star takes in a number and two strings and produces a new type of data, called an Image. (star50 solid red ) Try this in your interactions window.
What is the datatype of each of the values listed below Number String or Image?
Domain of a function is its input or the data the function expects. Examples: The function + has a domain of two numbers The function has a domain of two numbers The function star has a domain of a number and two strings We need to keep track of the required inputs for each function
By keeping a list of all the functions in a language, and their Domains, programmers can easily look up how each function is used. It s also important to keep track of what each function produces. For example, a program wouldn t use star if they were trying to produce a Number, because star only produces Images. The Range of a function is the data that the function produces.
CONTRACTS A contract has three parts: The Name Example: Star Domain Example: A number and Two Strings Range Example: Image The Contract is: ; star: Number String String Image
Write the contract for star in the back page of workbook
Here is the contract for a new function rectangle ; rectangle: Number Number String String Image What is the Name of this function? How many things are the Domain of this function? What is the type of each thing in the Domain? What is the Range of this function? Write the contract for rectangle in your contracts page
MORE IMAGE FUNCTIONS ; circle : ; ellipse: ; triangle: ; rectangle: Number Number String String Image ; text: String Number String Number String String Number Number String String Image Number String String Image Image Image ; rotate : ; scale: Number Image Number Image Image Image and dozens more! 12
( 3 + 4 ) = 7 What would the contract be of this expression? ; + : NUMBER NUMBER NUMBER Write the contract for +, -, *, / on your contracts page?
CONTRACT OR CODE? ; triangle : Number String String Image (triangle 100 outline blue ) ; square : Number String String Image (square (+ 200 5) solid red )
Figure out the domain and range of the following: rhombus right-triangle radial-star star-polygon Write the contract for each in your contracts page.
IMAGES CAN BE INPUTS You can use the function: flip-horizontal to flip your image from left to right CONTRACT ; flip-horizontal : Image Image CODE (flip-horizontal (right-triangle 50 20 outline red))
Convert the racket code to a contract and add to your contract page (flip-vertical (triangle 50 outline orange )) (scale 2 (text resize 10 purple )) (rotate 45 (text spin 30 green )) Experiment with the code in the interactions window