In Lesson 6, when we were learning about functions, we used them to have the robot trace out a grid of squares. One way to do this, among several, was to create a function that simply traced out one square. It could be very useful to have a general square-tracing function that, given a location and a side length, could trace a square of that size at that location. To do so we will need to learn how to create a function that has its own parameters. We will start by considering a simpler example: a square-tracing function that uses one parameter, the side length of the square, and traces a square with its bottom left corner at the point x = 10, y = 10.
|
Grid Size: S M L | Simple View: | MinView: |
|
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
|
The pre-placed code moves the robot to the point x = 10, y = 10 (with the trace off) and then calls the "traceSquare" function (defined at the bottom of the code), sending it a parameter value of 14. When the traceSquare function receives the value of 14, it inserts it into its "sideLength" parameter and then executes the function's code to trace the square. There's one bug in the code, however. Run the pre-placed code to see what happens, and then find and fix the bug. (See the hint if you get stuck.) |
|
|
|||||||||||
|
|||||||||||