Up until this stage, we’ve met the concept of Functions in coding a number of times. I’ve finally figured out that Functions in computer coding is DIFFERENT than the functions we teach in elementary school math.
The first time I came across the word, “Function” in terms of Coding, the definition given to me was:
- Function: A piece of code that can be called over and over
My response to that explanation was HUH?! After running into Functions a few more times, the light finally dawned for me when it was linked to Song Writing. We are now at Stage 13 of Code.org’s 20 Hour Coding Program. In this stage, we’re using our understanding of Functions in a very advanced and focussed manner by APPLYING them to solve puzzles.
If you look at the top image you’ll see a block with a star in the corner. The star indicates that this group of blocks is a Function. The group of blocks, in this case, Fills 5. The Function (indicated by the star) is the Function Defined. This means that the group of blocks together executes a series of actions. In other words, they define the function.
Underneath the starred block, you’ll see a green block that simply says, “Fill 5”. This green block is the Function Called. Every time you lay out this block, you’re calling the complicated Function above it to perform.
So a function needs two parts. Somewhere in the program, you have to define the Function (the stuff above in the workspace). Then, you have to call it (the green block below) when you need it.
In the above picture, there are two Functions defined. (We see both of them up top of the work space). We have one to Fill 5 and also one to Remove 7. In our program, we only called “Remove 7” as that’s the only function (action) that we needed to solve the puzzle.
I thought this above was a cute Function call: “avoid the cow and remove 1” (green block). Notice how big the Function Defined (up top) is and how small the Function Called (down below) is. That’s the benefit of using a function. We can now call on that huge piece of programming on the top over and over again easily, quickly, and without a lot of writing by just laying down a single block.
For a Function to be valuable, it has to be general enough to be able to be used in a variety of situations. If it’s too specific, it won’t be called very often. (Now, that’s easier said than understood. I’ll have to wait for some examples before understanding the difference between being general and being specific in a Function.)
I finally understand what is meant by defining a Function as a “piece of code that can be called over and over again”.
I wonder if a Function will make a program run faster (more efficiently). The more I learn, the more questions come to me. I feel like I’m a youngster again grappling with basic language and as I make more and more connections and answer my questions, the understanding is coming. This is a good exercise for teachers to go through—–learning like a child again. It makes me appreciate what children go through everyday as they learn math and what second language learners must go through too. Very humbling and a reminder to us, teachers, to be patient and understanding…
I believe I will start creating functions in my home:
- Function 1: take off your jacket and hang it up
- Function 2: clear your plate and put it in the dishwasher
- Function 3: do your homework, practice your music and do your reading
I’ll give each function a different card colour. When I want to call a function, I’ll flash the appropriate colour and save my breath! Why didn’t I think of doing this before 😉 ?