Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs."— Presentation transcript:

1 functions

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

3 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

4 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

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

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

7 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

8 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

9 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

10 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

11 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

12 recursion an function that calls itself…

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

14 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

15 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

16 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

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

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

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

20 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

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

22 branch becomes tree

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

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

25 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

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

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

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

29 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

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

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

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

33 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

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

35 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

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

37 o recreate the following turtle drawing

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

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

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

41 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

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

43 recursion Writing a function that calls itself

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

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

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

47 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

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

49 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: a collection of lines of code with a name that one can call. Functions can have inputs and outputs."

Similar presentations


Ads by Google