* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download - Office Mix
Survey
Document related concepts
History of the function concept wikipedia , lookup
Mathematics of radio engineering wikipedia , lookup
Function (mathematics) wikipedia , lookup
Location arithmetic wikipedia , lookup
Elementary mathematics wikipedia , lookup
Pythagorean theorem wikipedia , lookup
Transcript
functions functions functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs. functions o Functions are sub-scripts that let you write more complicated programs. o Anything that can happen in normal code can happen in a function. o However, variables are generally not shared across functions practice o Write a function that draws an equilateral triangle, then call it from the main portion of your program. o If you finish, try coming up with ways to call the function multiple times parameters o One powerful way to use functions is to add parameters. o These are variables that are passed between the caller and the function, and let the function act in different ways. inputs o inputs are the values passed when calling a function. (‘params’ tab in app) “x” is an input “x” is a number action sum(x : Number, y : Number) z := x + y “y” is also a z->post to wall number input you can use inputs in the function. They already exist when you enter the function. inputs o What will main do? action main code->sum(1, 2) code->sum(5, 7) action sum(x : Number, y : Number) z := x + y z->post to wall outputs o Outputs are the values returned when leaving a function. (‘returns’ tab in app) “r” is an output “r” is a number action sum(x : Number, y : Number) return r : Number r := x + y we assign the value to the output. This will be the value returned by sum outputs What will main do? action main var z := code->sum(4, 3) z->post to wall o action sum(x : Number, y : Number) returns r : Number r := x + y exercise 1 o write an action that computes the sum of 3 numbers and posts it to the wall. o write an action that returns the sum of 3 numbers exercise 2 o write an action that takes a “length” number and draws a pentagon using that length o add a parameter for the color and draw the triangle with that color recursion an function that calls itself… square o Start by drawing a square using a function called square. o We’ll add recursion to this on the next slide. square recursive private action square(side : Number, depth: Number) for 0 <= i < 4 do turtle->forward(side) turtle->left turn(90) recursion! square calls itself if depth > 0 then ▷square(side * 0.4, depth – 1) action main turtle->initialize ▷square (200, 4) squares recursive action square(length : Number, depth : Number) for 0 <= i < 4 do turtle->forward(length) turtle->turn(90) recursion! square calls itself if depth > 0 then ▷square(length * 0.4, d – 1) action main turtle->initialize ▷square(200, 4) smaller square going deeper into the fractal tree – first step 1. 2. 3. 4. 5. 6. 7. 8. 9. Move forward side Turn left 20 degrees Move forward side * 0.8 Move backwards side * 0.8 Turn right 40 degrees Forward side * 0.8 Backwards side * 0.8 Left 20 degrees Backwards side tree – first step Private action tree(side : Number, depth: Number) turtle->forward(side) turtle->left turn(20) turtle->forward(side*0.8) turtle->forward(-side*0.8) turtle->right turn(40) turtle->forward(side*0.8) turtle->forward(-side*0.8) turtle->left turn(20) turtle->forward(-side) how do we add recursion? o Do we want to add it at the end? o At the beginning? o Maybe we want to replace some aspect of the program we’ve already written? adding recursion Try replacing the 2 lines below with recursion (in the tree action): turtle-> move(side*0.8) turtle-> move(-side*0.8) branches o Let’s draw a branch… how do you draw 4:forward –c/2 this? 7: forward c/2 8: forward –c/2 9: right turn 45 10: forward -c 5: left turn 45 6: left turn 45 3: forward c/2 2: right turn 45 1: forward c branch action action branch(c : Number) // go to trunk turtle->forward(c) // rotate towards right branch turtle->right turn(45) // move to tip of the right branch turtle->forward(c/2) // go back to the top of trunk turtle->forward(-c/2) // cancel right turn turtle->left turn(45) // rotate towards left branch turtle->left turn(45) // move to tip of the left branch turtle->forward(c/2) // go back to the top of the trunk turtle->forward(-c/2) // cancel right turn turtle->left turn(45) // go back to base turtle->forward(-c) branch becomes tree tree action action branch(c : Number, d : Number) // go to trunk turtle->forward(c) // rotate towards right branch turtle->right turn(45) // move to tip of the right branch turtle->forward(c/2) if d > 0 then branch(c * 0.4, d – 1) // go back to the top of trunk turtle->forward(-c/2) // cancel right turn turtle->left turn(45) // rotate towards left branch turtle->left turn(45) // move to tip of the left branch turtle->forward(c/2) if d > 0 then branch(c * 0.4, d – 1) // go back to the top of the trunk turtle->forward(-c/2) // cancel right turn turtle->left turn(45) // go back to base turtle->forward(-c) exercises o o o o change the thickness of the trunk and branches based on the depth – it should get thinner as you draw deeper the branches change the color from brown (top level) to green (deepest level) instead of 2 child branches, use 4 child branches (tip: use a for loop). add small random variations to the turns and moves to make the tree look more ‘natural’. turtle exercise o Write a program that draws a square. Use a variable to represent the side length. o Once you’re done, choose a random color and a different pen thickness for each side. o You can also try drawing other shapes practice o Go back and add a parameter for side length to your triangle function. o Try calling your function with different values for that parameter. experiment o There are lots of cool geometric drawings you can make with turtle. Here are a few ideas to get you started: o Use functions within a loop so that they get called many times. o Draw circles, diamonds, or stars. o Try functions that don’t return the turtle to where it started. exercise o Write an app that draws a circle using twenty sides. o Make sure to use a variable to represent side length – you may be surprised to see how big or small your circle will be exercise 0 o Write an app that prints the squares of the numbers 0 through 100 to the wall Too easy? Try writing an app that finds the sum of those numbers instead. exercise 1 o o o o start a new turtle script create an new action, named ‘triangle’, that draws a triangle of length 200 action triangle() for 0 ≤ x < 3 do turtle->forward(200) turtle->left turn(120) in ‘main’, create a ‘for loop’ that calls the ‘triangle’ action 100 times turtle->initialize for 0 ≤ x < 100 do code->triangle after each ‘triangle’ call, rotate the turtle by 5 degrees turtle->initialize for 0 ≤ x < 100 do code->triangle turtle->left turn(5) exercise 2 o change the ‘triangle’ action to take the side length as an input, use that length when drawing the triangle action triangle (length : Number) for 0 ≤ x < 3 do turtle->forward(length) turtle->left turn(120) o in main, when calling ‘triangle’ in the for loop, increase the length by 5 per iteration var length := 20 for 0 ≤ x < 100 do code->triangle(length) turtle->left turn(5) length := length + 5 exercise 3 o change ‘triangle’ to take the side color as an input o in ‘main’, when calling ‘triangle’, use a random color exercise 4 o change ‘triangle’ to take the side thickness as an input. The unit of thickness is pixels. o in ‘main’, when calling ‘triangle’, increase the thickness on each iteration exercise 5 o change ‘triangle’ to take the number of sides as an input. For a triangle, #sides is 3, for a square #sides is 4, etc… o in ‘main’, increase the number of sides of the polygon being drawn on the screen on each iteration. o recreate the following turtle drawing o start by creating an action that draws a line of gears o then use it in a loop… o think like a turtle inputs/outputs “x” is an “number” input “y” is a “number” input action sum(x : Number, y : Number) return r : Number r := x + y “r” is an “number” output. Its value is what the function returns. action sub(x : Number, y : Number) returns r : Number What does ex1, ex2, ex3 do? r := x – y action add(x : Number, y : Number) returns r : Number r := x + y action ex1() var z := code->sub(10, 5) z->post to wall action ex2() var z := code->add(10, 5) z := code->sub(z, 6) z->post to wall action ex3() var z := code->sub(code->add(1, 2), code->add(4, -1)) z->post to wall var z := code->sub(code->add(1, 2), code->add(4, -1)) Always process the inner expressions first... We evaluate the first code->add call. var z := code->sub(3, code->add(4, -1)) Then the 2nd add call var z := code->sub(3, 3) and finally, the call to ‘sub’ var z := 0 last turtle • once you are done with the basic shape, add your own colors/mods/customizations • publish your script so that we can demo it to the class! recursion Writing a function that calls itself sum sum 1 to 2 = 1 + 2 sum 1 to 3 = 1 + 2 + 3 = (sum 1 to 2) + 3 sum 1 to 4 = 1 + 2 + 3 + 4 = (sum 1 to 3) + 4 sum 1 to 5 = _____________ = (sum 1 to __) + ___ sum 1 to 6 = _____________ = (sum 1 to __) + ___ Now let’s say n is any positive number sum 1 to n = 1 + 2 … + n – 1 + n= (sum 1 to ___) + ____ factorial factorial(n) = n! = 1 * 2 * … * n 1! = ____________ 2! = ____________ = _____ ! * _______ 3! = ____________ = _____ ! * _______ 4! = ____________ = _____ ! * _______ Now let’s say n is any positive number, n! = ____________ = _____ ! * _______ what is recursion? o Recursion is when a action calls itself o Here’s an example that calculates sum 1 to n: action sum(n : Number) returns r : Number if n = 1 then r := 1 else r := n + code->sum(n-1) recursion is awesome o Recursion lets you write extremely complicated programs succinctly o Recursion lets you write programs that would be nearly impossible otherwise o Recursion is an awesome concept once you really understand it recursion is hard o Keeping track of what a recursive function does is confusing o Writing functions that work at multiple levels of recursion is challenging o Infinite recursion can crash your program • Look at the sum function from a few slides ago. What happens when n = 0? factorial o Try changing your program to calculate n factorial instead of the sum of the numbers up to n. o Remember n factorial, or n!, is equal to 1 * 2*…*n