We are now onto Stage 15 of this 20 stage learn to code program by Code.org.
Click here to see my Function pictured above run.
Up until this stage, the definition of Function was this one:
- Function: a piece of code that can be called over and over
In this stage (Stage 15) , the definition for Function is this one:
- Function: a sequence of instructions
If you look at the image at the top of this blogpost, you will see that there are two Function Definitions:
- To draw a square
- To draw a circle
We know that it is a Function Definition because it has a star in the corner of the block. Inside the Functions are sequences of instructions. Having these two sequences of instructions is not enough. We still need to “call” the Function. We call it (activate it) by dragging the Function into the program space. In other words, if we want to draw a square, we drag out the block that says “Draw a Square”. This is what I have done (see the green block above).
We don’t want to draw a circle so we stop there. If we wanted to draw a circle, we could have to drag that block out.
In Stage 15, we are learning to define functions by doing more Artist puzzles. That means we are trying to figure out and insert the little bits of instructions that appear inside the starred block.
Click to see my little program to draw a Triangle run. You can see the code below:
We now move on to working with multiple functions and stacking them in sequence to draw a complex design. Click here to run what is shown below.
The next step is to name my own Function Definition and to put the sequence of instructions inside it. In this case, I named my Function Definition “Draw a House” and then I pulled out the block that said “Draw a House”.
Click here to run it and take a look at the program below:
I think we’ve spent quite a lot of time on Functions. The next set of puzzles in Stage 15 is to put in Parameters.
- Parameters: things you add to the Function so you can customize it
Up until now, we dragged out a “Draw a Triangle” block when we wanted a triangle. We probably want to specify the colour of the triangle or the size of the triangle so that we have more control over what we are drawing. Adding in these customizations is called passing in a Parameter. (“Colour” and “Size” are the variables in this case.)
If you look at the image below, you will see one Function with the word “with Length” near the star. It’s the “Draw a Square” block. The “with Length” is allowing us to put in a parameter that will customize the length. The other block called “Draw a Triangle” doesn’t allow for Parameters. Our job for this puzzle is to change the “Draw a Triangle” block so it can take Parameters. In this case, we click on the star in order to do this.
Click here to make my program run.
See the image below. I’ve changed my “Draw a Triangle” block so it can take Parameters.
The rest of the puzzles in Stage 15 are exercises needing more and more Function Definitions and we have to customize these Function Definitions so there are variables and so the variables can take Parameters. Then, we drag out the Function blocks to activate the Function Definitions and actually specify the number (Parameter) we want.
As always, it’s much easier to do than to explain. Everything has been scoped and sequenced very well so I’ve never felt like the increments in learning were too big. Like Goldilocks, they have all been “just right”.
Click to run the program below:
Click to Run the program below and I’ve used a counter (block in pink) so I don’t have to write so much coding:
I’ve now written 1,390 lines of code since the beginning of the 20 Hour “Hour of Code” course. Wheew!