Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions. functions – learning targets I will be able to understand how to trace function I will be able to learn how to lavage inputs and outputs with.

Similar presentations


Presentation on theme: "Functions. functions – learning targets I will be able to understand how to trace function I will be able to learn how to lavage inputs and outputs with."— Presentation transcript:

1 functions

2 functions – learning targets I will be able to understand how to trace function I will be able to learn how to lavage inputs and outputs with functions I will be able to learn how to write algorithms and methods in constructing statements

3 functions– common core state standards 1.1 Innovate: Demonstrate creative thinking, construct knowledge and develop innovative products and processes using technology. 1.3 Investigate and Think Critically: Research, manage and evaluate information and solve problems using digital tools and resources 2.4 Adapt to Change (Technology Fluency): Transfer current knowledge to new and emerging technologies L.11-12.3. Apply knowledge of language to understand how language functions in different contexts, to make effective choices for meaning or style, and to comprehend more fully when reading or listening. RI.11-12.1. Cite strong and thorough textual evidence to support analysis of what the text says explicitly as well as inferences drawn from the text, including determining where the text leaves matters uncertain 9-12 APPD The ability to solve problems is greatly enhanced by use of mathematics and information technologies 9-12 INQF Communicate Science is a human endeavor that involves logical reasoning and creativity and entails the testing, revision, and occasional discarding of theories as new evidence comes to light. S-CP.9. Use permutations and combinations to compute probabilities of compound events and solve problems. N-Q.1. Use units as a way to understand problems and to guide the solution of multi-step problems; choose and interpret units consistently in formulas; choose and interpret the scale and the origin in graphs and data displays. Educational Technology Language Reading Science Math

4 functions functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.

5 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

6 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

7 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.

8 inputs o inputs are the values passed when calling a function. (‘params’ tab in app) action sum(x : Number, y : Number) z := x + y z->post to wall “x” is an input “x” is a number “y” is also a number input you can use inputs in the function. They already exist when you enter the function.

9 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

10 outputs o Outputs are the values returned when leaving a function. (‘returns’ tab in app) action sum(x : Number, y : Number) return r : Number r := x + y “r” is an output “r” is a number we assign the value to the output. This will be the value returned by sum

11 outputs o What will main do? action main var z := code->sum(4, 3) z->post to wall action sum(x : Number, y : Number) returns r : Number r := x + y

12 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

13 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

14 recursion an function that calls itself…

15 square o Start by drawing a square using a function called square. o We’ll add recursion to this on the next slide.

16 square recursive private action square(side : Number, depth: Number) for 0 <= i < 4 do turtle->forward(side) turtle->left turn(90) if depth > 0 then ▷ square(side * 0.4, depth – 1) action main turtle->initialize ▷ square (200, 4) recursion! square calls itself

17 squares recursive action square(length : Number, depth : Number) for 0 <= i < 4 do turtle->forward(length) turtle->turn(90) if depth > 0 then ▷ square(length * 0.4, d – 1) action main turtle->initialize ▷ square(200, 4) recursion! square calls itself smaller square going deeper into the fractal

18 tree – first step 1. Move forward side 2. Turn left 20 degrees 3. Move forward side * 0.8 4. Move backwards side * 0.8 5. Turn right 40 degrees 6. Forward side * 0.8 7. Backwards side * 0.8 8. Left 20 degrees 9. Backwards side

19 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)

20 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?

21 adding recursion Try replacing the 2 lines below with recursion (in the tree action): turtle-> move(side*0.8) turtle-> move(-side*0.8)

22 branches o Let’s draw a branch… how do you draw this? 1: forward c 2: right turn 45 3: forward c/2 4:forward –c/2 5: left turn 45 6: left turn 45 7: forward c/2 8: forward –c/2 9: right turn 45 10: forward -c

23 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)

24 branch becomes tree

25 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)

26 exercises o change the thickness of the trunk and branches based on the depth – it should get thinner as you draw deeper the branches o change the color from brown (top level) to green (deepest level) o instead of 2 child branches, use 4 child branches (tip: use a for loop). o add small random variations to the turns and moves to make the tree look more ‘natural’.

27 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

28 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.

29 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.

30 circle o solution: “create a turtle program that draws a circle” turtle→initialize for 0 <= i < 360 do turtle→move(1) turtle→turn(1)

31 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

32 question o What does this do? for 0 <= i < 100 do i→post to wall

33 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.

34 exercise 1 o start a new turtle script o create an new action, named ‘triangle’, that draws a triangle of length 200 action triangle() for 0 ≤ x forward(200) turtle->left turn(120) o in ‘main’, create a ‘for loop’ that calls the ‘triangle’ action 100 times turtle->initialize for 0 ≤ x triangle o after each ‘triangle’ call, rotate the turtle by 5 degrees turtle->initialize for 0 ≤ x triangle turtle->left turn(5)

35 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 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 triangle(length) turtle->left turn(5) length := length + 5

36 exercise 3 o change ‘triangle’ to take the side color as an input o in ‘main’, when calling ‘triangle’, use a random color

37 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

38 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.

39 o recreate the following turtle drawing

40 o start by creating an action that draws a line of gears o then use it in a loop… o think like a turtle

41 inputs/outputs action sum(x : Number, y : Number) return r : Number r := x + y “x” is an “number” input “y” is a “number” input “r” is an “number” output. Its value is what the function returns.

42 action sub(x : Number, y : Number) returns r : Number 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 What does ex1, ex2, ex3 do?

43 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 2 nd add call var z := code->sub(3, 3) and finally, the call to ‘sub’ var z := 0

44 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!

45 recursion Writing a function that calls itself

46 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 ___) + ____

47 factorial factorial(n) = n! = 1 * 2 * … * n 1! = ____________ 2! = ____________ = _____ ! * _______ 3! = ____________ = _____ ! * _______ 4! = ____________ = _____ ! * _______ Now let’s say n is any positive number, n! = ____________ = _____ ! * _______

48 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)

49 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

50 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?

51 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


Download ppt "Functions. functions – learning targets I will be able to understand how to trace function I will be able to learn how to lavage inputs and outputs with."

Similar presentations


Ads by Google