How to make tea: an illustrated introduction to functions

5 minute read, 1041 words

Filed under: data-science programming

Last updated on: September 21, 2021

In an earlier post we saw how we could save a series of programming commands in a script and then source that script whenever we needed to execute the same sequence without having to type it all out again.

Functions are a similar concept in programming but with a key added feature. Instead of simply running the sequence of commands, you can specify additional parameters that the can be used within those commands.

For instance, consider the following function sum(32, 15). sum is the name of the function and 32 and 15 are the arguments that were passed to the function. If we executed this function we would expect it to return the value 47. If we changed the values of the arguments to some other numbers then the output of the sum function would change accordingly. In programming terminology, whenever we use a function in our code it is referred to as being called with its arguments. Here we call the function print with the argument “Hello World”.

Functions are a simple but extremely powerful tool with a lot of benefits.

  • Well named functions enhance the readability of your code the same way as a heading might in a text document. When a programmer uses functions like sum or print, the reader clearly understands what they are seeking to achieve. This is in contrast with a series of commands that implement the nitty-gritty details of adding two numbers or printing something to the console, which often are confusing to someone else who is not necessarily familiar with the logic behind the detailed steps.

  • By encapsulating a series of steps in a function, you also reduce the likelihood of errors that could occur if you had to copy and paste the same set of commands in different places in your code.

  • Eliminating the need to copy paste commands also makes your code much easier to manage, since if you realize you have to modify a particular command you only have to do it within the function definition, whereas if you did not use a function, you would have to find all the places where that command appears and then change all those instances.

Let’s make some tea

Let’s explore why functions are useful and how to go about building them by teaching a computer to make tea. Imagine this computer is sending instructions to a robot.

Now, asking a computer to make tea is very different from asking a fellow human.

Your friend doesn’t need step by step instructions and would think you were trying to pull a joke if you directed them to “put the kettle on the stove and wait for the water to come to a boil. And then open the cupboard to get a cup”. You would instead simply declare, “Make me some tea, please”.

Unlike your friend a computer needs very precise step by step instructions to make tea. So let’s look at two ways to provide this instruction. One without functions and another using functions.

Let’s start by turning on our computer and asking it to boil some water for the tea.

First you need to get the kettle from your cupboard. This would involve going to the cupboard, opening it, and then pulling out the kettle from the cupboard and then closing the cupboard.

Now we need to take the kettle to the tap and fill enough water for a cup.

Next let’s give all the instructions to boil this water.

Now that we have boiling water, we need to go back to the cupboard to get a cup to pour the water into.

Now, the only thing missing is a tea bag. Let’s go back to the cupboard to fetch that as well.

Now we need to place the teabag in the cup and pour the water in.

Enjoy your tea!

The function way

Functions can be defined as follows

Functions are self contained modules of code that accomplish a specific task

So how do we recognize when to use a function in our code???

The biggest hint is when you notice yourself copying and pasting code from one part of your script or notebook to another. This is a signal to stop and consider if it is worth the effort to encapsulate that particular sequence into a function. For instance, in our series of steps for the computer, the sequence where we go to the cupboard to fetch something is repeated and can be replaced with a function.

Let’s create a function called get_item that takes the item that we would like to get from the cupboard as its argument. Notice how in the function definition, for step 3, where the computer needs to take a particular item, we use item_name instead of a specific item like a kettle or a cup as we did previously.

This is the key design feature that allows us to use functions repeatedly with different parameters. An argument like item_name is simply a placeholder in the function definition that allows anyone calling it to specify values that they want.

Now we can replace all the repeated cupboard sequences with our get_item() function. And instead of the item_name, we use the name of the specific item that we would like to fetch from the cupboard and internally the function will replace item_name with the name of the item that we pass as an argument for the function. So get_item(“cup”) will fetch a cup from the cupboard.

Now that we have replaced the repeated sections, we can clean up our code a little bit. The code is now much smaller and easier to understand.

This mode of writing code using functions is also called declaritive programming i.e. we are “declaring” what we would like to do rather than giving step by step instructions. The latter is known as imperative programming.

More functions

Now imagine if you were instead teaching the computer to cook. And making tea was one of the many other things that are included in this huge program. Some of the sequences in making tea, such as that for boiling water or putting an item into a vessel etc. are also likely to be repeated elsewhere. How would you write a function to encapsulate these actions?