How many steps does it take to make a peanut butter and jelly sandwich? Fewer if you use functions. A function is a list of steps in a program that are all wrapped up together. Functions help programmers logically group pieces of code to make it easier to read and reuse in multiple places.
Try out the student’s instructions for creating a peanut butter and jelly sandwich below.
The students at Paul Public Charter Middle School grouped repeat steps like, Spread, into functions. The students defined the function Spread as the process of scooping out the condiment from its container with a knife and smearing it onto one side of a plain piece of bread.
The students realized that this step would be repeated twice, once to spread the peanut butter and once to spread the jelly onto each slice of bread. They determined SPREAD was a function and could be reused to make their code easier to read, but how would they know which condiment to spread?
Parameters are extra details computer scientist add to their code, to change the way it behaves. Students added parameters in parenthesis to specify which condiment would be used each time the ‘spread’ function was called.
Turns out recipe instructions are just like lines of code. Can you write a code for your favorite dish? Try using functions to shorten the directions.