The Purpose of Using Functions

This week in school, our discussion assignment was to discuss the importance and advantages of using functions in programming. I’ve been using Khan Academy for about 6 months now for studying math, and computer science outside of school, So I’ve seen a lot of beginners on the site who don’t understand the purpose of using functions. Hopefully this article sheds a little light on this topic for those who also don’t understand.

The most common way to explain the importance and advantage of using functions is that they allow us to create reusable code. This idea is really the fundamental purpose of functions after all. But the real foundation that makes functions reusable is called encapsulation. 

Encapsulation, sometimes referred to less technically as information hiding, is the idea of not caring how a function works, only what it does.

One analogy that I really like, is one I heard from YouTube star, Prof. Mehran Sahami at Stanford University. In the CS106a course, he explains encapsulation as a toaster. When you use a toaster, you don’t care about how it makes toast, you only care about the fact that it does make toast. You care about the what not the how. You know that when you put in a slice of bread, and push down the lever, that the toaster will return to you toast. This also leads us into understanding the importance of parameters and return values in functions and how they help support encapsulation.

Continuing the toaster analogy, when you put toast inside of one, it’s the same thing as passing an argument to a function’s parameter. In this case the toaster is like a function that takes two parameters – each slot is a parameter. The bread is the argument, and the toast is the return value.

Think of the slots in the toaster as parameters and remember that they’re local variables. They accept certain objects as their arguments like a piece of bread or a bagel. But the slots are used to do some processing and return back a toasted piece of bread or bagel, and the slots themselves are not returned to you. The slots are local to the toaster, and the toaster uses them to return you toast, but you don’t care how. You only know what can be passed in, and you know what comes out when it’s done.

Let’s look at a real example of a function in Python that we all should be very familiar with by now – print(). It’s a little different from our toaster because it has no practical limit to the number of arguments we can pass.

For example you have probably done something like this already.

print("The area of the triangle is: ", area)

Here we only passed two arguments to the print function’s parameters, but we could have passed many more. We all know that whatever we pass to the print function gets printed to the screen as a string, and we understand how to use it.

What we most likely don’t know, is how our arguments get printed to the screen. Which is fine. You don’t need to. You don’t need to know how a car is built to drive it, and you certainly don’t need to know how the print function was built to use it either. That’s the whole point of a function. It encapsulates some code to be reused later, and does not require any knowledge of the inner workings to be used. Parameters and return values are what create the interface we use to interact with our functions. Just like a steering wheel is a part of the interface in a car.

If you can remember from our textbook, the way we use and interact with a function is called an interface. This is really were the purpose of using functions becomes realized, if it hasn’t already. An interface is what allows other people to reuse your code in forks of your project if you’re working with a team, or their own projects if you make your interface public for others.

Staying on the topic of interfaces, functions are the building blocks of libraries and frameworks. Both are collections of functions that achieve a set of common tasks. Programmers rely on libraries and frameworks to do work that someone else has already done for them. You probably don’t care about how to make a graphics window, but you know you need one for your program, so you import a framework that makes it easy and painless.

Instead of making your own functions to do advanced mathematics, you would probably just import a math library of functions that someone else has already made and tested.

Further more libraries and frameworks allow for a community of programmers to use the same functions/interface. This makes them powerful because you can ask questions about any problems you may be having with a particular function. Without functions and their re-usability, this would not be possible.

Even as you learn to write your own programs which are probably pretty small right now, functions are handy. They save you a lot of typing, and even more importantly a lot of debugging. When something goes wrong in a function it’s easy to fix. But if you have too much repeated code and refuse to encapsulate potentially reusable code, it can be tricky to figure out what went wrong.

Happy coding :)

Leave a Reply