Before we go on, here is one more thing on functions that return values. When you were using the `rotate()`

function, you learned the value passed in is not degrees of rotation but something called radians. Radian measures are based around the value PI (3.1415) representing rotation.

We can convert from degrees to radians by multipling your given degrees by PI and dividing by 180. For example converting 45 degrees to radians:

45 * 3.1415 / 180 = 0.785375

or 180 degrees.

180 * 3.1415 / 180 = 3.1415

The next activity talks about creating a function that returns a value to convert degrees to radians. You have used functions that return values when you used my textHeight() function. Calling that would return a number indicating the number of lines high the text is. You have also called functions that take a value. The `rect`

function takes 4 values to describe where a rectangle should be drawn.

Here is an example of combining those ideas for a function that squares a number. Squaring a number means multiplying a number by itself:

1 2 3 | function sq(x) { return x * x; } |

You could use the function like this:

1 2 3 | x = sq(4); y = sq(10); print(sq(1)); |

Complete Activity 38

There are a couple of other types of loops but we will only talk about one other: the “for” loop.

Here is an example:

1 2 3 4 5 6 | var total = 0; var limit = 5; for(var i=0; i <= limit; i++) { total = total + i; } |

(NOTE: this wasn’t covered earlier but “i++” is a shorthand for “i = i + 1;”. You can write “i–” as well which is the same as “i = i – 1;”.)

The “for” takes an expression in parenthesis with three semicolon separated parts:

1 | for( ; ; ) {} |

- Statement 1 is run before the loop begins. You typically initialize your loop counter there.
- Statement 2 is tested each time before the loop runs. If the result of statement 2 is true, then the loop will run.
- Statement 3 is run after each time through the loop. We typically use this statement to increase or decrease the loop counter.

The for loop is sometimes more convenient to write than a while loop however you can write equivalent programs using either method.

Complete Activity 39

Complete Activity 40