Intermediate Functions
Contents
Introduction
A function is a group of code that you can re-use elsewhere in your program. As coders, we often want to be able to re-execute blocks of code when we are writing programs, without having to re-write the block of code entirely. Functions give us a way of grouping code together and giving it a name, so that we can call it by that name later.
To create a function, we must first declare it and give it a name, and then follow it by a function definition. We could put any code inside that function – one statement, multiple statements – depends on what we want to do.
In this function, we are just getting two nectars from the flower in this Bee: Functions puzzle on Code.org:
Now, if all we do is declare the function, nothing will happen. In order for the program to execute the code that’s inside the function, we actually have to “call” the function:
And then we can call it whenever we wanted, how many ever times we want!
What is it?
A function is a group of code that you can re-use elsewhere in your program.
The function definition is the actual code of the function, while the function call is used when we tell the functions to run.
A procedure is just another way of saying function.
Bee Functions
Remember that functions are reusable blocks of code. Using the bee environment in Code.org’s Bee: Functions puzzle, you’ll use and modify functions to help the bee collect nectar and make honey. Some puzzles will even require you to create a function from scratch. As a visual cue, you’ll find the function definition inside a grey box on the screen.
Procedures in Lightbot
Another word for functions is Procedures . Players in Lightbot might use procedures when they don’t have enough space in the MAIN block to solve a problem. Procedures are helpful for extracting patterns and re-using a set of commands multiple times. Try the Stage 2: Procedures puzzle in Lightbot to practice creating procedures.
Functional Keychains
Sometimes you want to do certain instructions over and over again. That’s where functions come in handy! Group all repeated instructions into one place, give them a simple name, then you can call that entire group at the same time just by using the name you gave it.
In this activity, you’ll make a keychain out of string, beads and a special charm. To do this, you’ll follow a series a repetitive steps and then identify certain sets of “skills” that are duplicated several times. Later, you can attach your keychain to your backpack or to a set of keys.
Steps to make the keychain:
1. Put a bead on the string, then tie a knot. Put another bead on a string, and tie another knot. Then, put a spacer on the string, then tie a knot. After that, do those steps again: Bead, knot, bead, knot, spacer, knot. Finally, add on the special charm and tie one last knot.
2. This routine takes quite a few steps, so it may help to sing or rap the steps with your team to remember the order.
-
-
- “Bead, knot, bead, knot, spacer, knot.
-
-
-
- Bead, knot, bead, knot, spacer, knot.
-
-
-
- Special charm, final knot.”
-
3. Write one instruction per line in the “Program” area using a pencil. You’ll notice quickly, though, that you have more steps than lines available.
4. Did you notice anywhere in your program with several sets of identical instructions? Move one or more of these sets into the “Skills” areas below; this is a function.
5. Rewrite the original program, using the skill names instead of actually writing out the group of instructions that the skills describe. By the way, there are several ways to write the program. Share your new program with your team and hear the many solutions your team has written.