In programming, we often use the same code more than once to repeat actions or add onto in order to execute more complex tasks. Functions allow us to teach the computer how to do something one time and easily reuse the code later. Students are introduced to functions in planet Moongarden.
Definition: A function is a sequence of commands that can be reused together later in a program.
Functions name the sequence of commands and dictate what code is executed. Functions also allow for separation of code, so we can easily use it again throughout a program.
Programmers often say; Don’t Repeat Yourself. You can think of this as an acronym to help you understand the purpose of functions: keep your code DRY!
Real life Example: As people, we are always learning and building on what we already know and can do. We add to a foundation to complete more complex processes in life- this is a lot like a function: piecing basic actions together to carry out more complex tasks.
In sports you can think about dribbling a soccer ball - putting together running and kicking, and naming it as one new action: Dribbling.
A great example of this in the classroom is “Criss Cross Applesauce”: we piece together separate actions to make one and name it something that students will remember. Students will learn it once and continue to use it throughout the school year.
Criss Cross Applesauce Function:
Once students learn “criss-cross applesauce”, we don’t have to instruct them step by step each time each time we say it- students know what to do when they hear it.
In programming, functions allow us to repeat a sequence of code without having to write the code over again. This allows us to simplify a complex action by using only one command, just like in our criss-cross applesauce example.
During the final part of each functions lesson, students will independently practice identifying patterns of commands that are used more than once in the mazes.
In Kodable, students will use functions if they recognize the same pattern used repeatedly. They can store commands once and reuse them when needed.
Our code to complete the maze is shortened and we avoid repeating ourselves by using the function twice to complete the maze. We command our fuzz to go Right, use our FUNCTION: which is up, right up, Right, we use our FUNCTION again, and Right. Instead of having to use 9 commands, we only use 5 and we earn 3 stars!
Just like this, we can teach the computer how to carry out a sequence of steps for one specific action in a computer program. We name it so we can use it later when we want the computer to do it again.
For example, if we want to write a program that brings up a window that says “hello,” to a user, we can write a function for it. Since there are two commands that the computer will need to carry out to do this, a function will prevent us from having to instruct the computer step by step each time we want it to say hello:
First, we name the function “Print and alert”, because that is what we want the computer to do and we’ll need to use the name of our function to do it again later.
Instead of having to write code for each step the next time we want the computer to perform this task, we just call the function “print and alert” and the computer will automatically bring up the window that says “hello.”
Why We Use Functions: Functions allow us to teach the computer how to do something one time and easily reuse the code to do it again later. Remember, Don’t Repeat Yourself!