Understanding Functions in Programming with Examples
Explore the concept of defining functions in programming with examples using solid green triangles, blue circles, and solid color circles. Learn to write contracts, provide examples, and define functions step by step. Discover how functions work in algebra and create your own function that adds 1 to a variable.
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 6 OF BOOTSTRAP DEFINING FUNCTIONS 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
I LOVE GREEN TRIANGLES Wescheme
Defining Functions gt will be a function that makes a solid green triangle ( gt 10) would be a shortcut for ( triangle 10 solid green ) gt is the function, 10 in the input-domain
Steps to define a function 1.Write a contract 2.Give Examples 3.Define Function ????:?????? ????? Turn to page 9: Fast Functions Lets work through writing a contract together
Defining Functions Write a function gt , which takes in a number and gives back a solid, green triangle of the given size. gt Number size Image gt gt 10 11 10 (triangle solid green ) (triangle solid green ) 11 gt size ((triangle solid green ) size 5
Defining Functions Confession: I lied. I actually prefer blue circles! Write a function bc , which takes in a number and gives back a solid, blue circle of the given radius. Image bc Number radius bc bc 16 99 16 (circle solid blue ) (circle solid blue ) 99 bc radius ((circle solid blue ) radius 6
Define a function: dot which takes in a color (domain) and produces(range) a solid circle of the given color with a radius of 20 (define (dot color) (circle 20 solid color))
How does this work in Algebra? define f x + x 1 1. Name of function? 2. How many variables? 3. What is the name of the variable(s)?
Define a function f that takes in a variable x and adds 1 ?????? ? ? + ? 1 In algebra in written as ? ? = ? + 1
Define a function g that takes in a variable r and multiples it by 20 ?????? g r r 20 In algebra in written as ? ? = ?(20)
Define a function h that takes in a variable x and divides it by 2 ?????? ? / ? 2 ? =? In algebra in written as 2
Page 38: Complete function definitions
Rocket-height rocket-height rocket-height Number Number Multiply the number of seconds by 7 to find the height radius seconds 105 15 (* 7 15) (* 7 289) rocket-height 289 15
Google: Wescheme Rocket-Height Follow 1st link Can you fix the game? Wescheme