It’s been a little while since I wrote a post entirely just about how to teach certain things in programming. This post is about how to teach functions in programming to students. While my strategy for teaching functions is not entirely unique, I have found that over the last few years I have really refined it. I also have found a lot of success with it, over a wide range of ages as well. For some students who have already had advanced mathematics classes, functions can be quite simple. For younger students though, you will need to be a bit more creative. Both age groups benefit a lot from treating functions like math though, since even very young students understand addition and subtraction. Which is simply just a function with two parameters.

The two aspects of teaching functions

When I am teaching functions to students, I always use the same strategy. This strategy has proven to be very adept at getting the main ideas across. I also approach functions in a language agnostic way. This is due to the fact that the specifics of a language are unimportant when discussing how functions work. In the examples I will show below, I will use Python as I prefer it, but I have created these examples in PHP, C++, Java, Scratch, and JavaScript before. The idea works in any language.

The main idea you should approach when teaching functions is to focus on the two aspects of functions. The first aspect is the actual syntax of the functions, as this is quite similar between languages in a theoretical way. As in, all functions have parameters, return values, and a name. Of course there may be exceptions to this, but for the languages students learn, this is true. The second aspect is the theory behind why functions are useful, and how we tend to use them in practice.

This article is mostly focusing on the first aspect, as the second one varies drastically depending on the experience or age of your students. It also can vary quite a bit depending on the language, as languages like Python introduce functions very quickly. Another language with a heavy emphasis on functions early is Java. So if your students start with Java, then functions is something they may already have an instinctual understanding of.

How I teach the first aspect

The way I teach the first aspect of functions is to use mathematics as a metaphor. Students are very used to parsing statements like “5+7=12”, and should have no issue understanding it. We can use this as an advantage when teaching functions. A lot of confusion when students are first starting to program comes from the fact that everything seems so foreign to them. They can’t really find a way to make programming fit into the forms of other knowledge they already have. I believe one job we have as teachers is to slowly remove this idea. Many ideas in programming come from other fields of study, and students are way more familiar with the fundamentals than they originally think they are. This method of teaching functions is a good way to illustrate this.

The way I begin explaining this is to show an example of an addition function like shown below.

def add(x,y): return x+y print(add(5,7))

This is a good example of one of the most basic functions possible, that still uses most of the features functions have. The first thing I do is explain that the two terms we are adding are the parameters ‘x’ and ‘y’. I tend to explain this from left-to-right. So ‘x=5’ and ‘y=7’. I then discuss how to the plus sign is the function. Then by saying ‘5+7’ we are calling the ‘add’ function and passing it two parameters, ‘5’ and ‘7’. Of course, the return value is then the sum of the two terms. The biggest benefit of this technique is just how simplistic it is. That is really why it works so well, students of any age can see how they have been using functions since they started learning addition.

The next step is to start combining operations like “5+7-8” and show how that is written in code, this helps students understand the order functions are called in. It also helps them understand how to use multiple functions together. Hopefully you get a chance to try this in class!

Extra resources for how to teach functions in programming to students

For younger students, one resource I love to use for functions is Google’s Blockly games. The music game especially is one of the best resources I have found for teaching functions. For older students, I recommend creating function call puzzles. Something like this can always be fun, see if you can figure out the result of the following Python code…

def x(y): return y+3 def q(y): return y*3 def z(y): return x(y)+q(y) print(z(5))

These are the types of puzzles I use to help my students think about the function call stack, how parameters work, and how return values work. This is also a great way to have students practice working out code on paper.

Conclusion

I hope this post about how to teach functions in programming to students gave you some new ideas, or that you just enjoyed reading it. If you are interested in some other posts about programming. I would recommend reading How I started Programming. If this works out for you in class, or if you have any questions about how to use idea. Leave a comment below.

CodingOverload also has a monthly series called Open Source Project of the Month, where we talk about some really cool and unique open source projects you might find useful in class.