You Already Know How to Code: Part 3

In Part 2, I showed you how you already know about variables, data types and operators. Here’s even more you already know about coding.

You Already Know About Functions

“Go easy on me, I need to take this one step at a time…”

If you’ve ever performed a task that required multiple steps, you’ve enacted a function. For example, take the task ‘changing a lightbulb’. Despite its deceptively simple name, ‘changing a lightbulb’ requires several separate steps:

  1. Turn off the power at the fusebox
  2. Remove the old lightbulb
  3. Fit the new lightbulb
  4. Turn the power back on at the fusebox
  5. Test the light now works by flicking the switch

What’s more, these steps may vary depending on where the light is. For example, for a ceiling light, you may need to add the steps, ‘fetch a stepladder’ and ‘put away the stepladder’.

When you tell someone you are going to ‘change a lightbulb’, you don’t tell them all the individual steps. But you know them, and you know that you have to perform them. The individual steps are encapsulated in the task, ‘changing a lightbulb’.

You also know to adapt the steps if the lightbulb is high up (you’ll need a stepladder) or if the bulb has been on for a while before blowing (you’ll need to allow it to cool before removing).

Perhaps you need to change more than one lightbulb. In that scenario, you know that you need to repeat the same steps again.

‘Changing a lightbulb’ is a collection of individual tasks, which collectively form the overall task. This is exactly what a function is.

Let’s see our ‘changing a lightbulb’ task as a python function:

def change_lightbulb():
    fusebox = 'off'
    remove_old_bulb = True
    fit_new_bulb = True
    fusebox = 'on'
    test = True
    print('Lightbulb changed!')

The def keyword is used to declare a function. The function being declared here has the name change_lightbulb. The steps to change the lightbulb are represented as setting variables (we re-learned about those in Part 2), for simplicity. If we want to run this code, we need to call the function like so:

change_lightbulb()

The output is:

Lightbulb changed!

Parameters

That’s all well and good, but what if the lightbulb is high up and we need a stepladder? Do we need another, separate function that includes getting the stepladder? Well, that is the beauty of functions. They’re flexible. We can add parameters to the function, to allow for different scenarios. Parameters are placeholders for values that are passed to the function and used by the function to perform tasks slightly differently.

For our lightbulb-changing example, what parameters could we pass in to the function? Well, there a couple of things we’ve already noted could be different in our lightbulb-changing scenario. Perhaps the light is on the ceiling, or the bulb is too hot to change right away. We can pass two parameters that tell the function the specific situation. For example, the parameter light_on_ceiling can be used to hold a boolean value (we re-learned about those in Part 2) that denotes whether or not the light is on the ceiling. The parameter bulb_is_hot could hold another boolean value that indicates whether or not the bulb is hot.

We define the function with the new parameters like this:

def change_lightbulb(light_on_ceiling, bulb_is_hot):
    fusebox = 'off'
    remove_old_bulb = True
    fit_new_bulb = True
    fusebox = 'on'
    test = True
    print('Lightbulb changed!')

The parameters go in between the brackets for the function definition. Inside the function, they are used like variables. The function here has not been updated to use the parameters yet, so let’s add some code to use them to change how the function performs the tasks:

def change_lightbulb(light_on_ceiling, bulb_is_hot):
    fusebox = 'off'
    if light_on_ceiling:
        print('Getting stepladder')
        get_stepladder = True
    if bulb_is_hot:
        print('Waiting ten minutes')
        wait_ten_minutes = True
    remove_old_bulb = True
    fit_new_bulb = True
    fusebox = 'on'
    test = True
    if light_on_ceiling:
        print('Putting stepladder away')
        put_away_stepladder = True
    print('Lightbulb changed!')

The function is using the parameter variables in conditional statements (we learned how we already knew about those in Part 1). We only get (and put away) the stepladder if the light is on the ceiling. We only wait for ten minutes if the bulb is hot. Now we have some flexibility in the tasks that the function performs, but all encapsulated in just one function.

To run this function, we call it with our parameters evaluated into arguments (i.e. we set the parameter names equal to the actual values we want the parameters to be, which can change every time we call the function):

change_lightbulb(light_on_ceiling=True, bulb_is_hot=False)

This gives the output:

Getting stepladder
Putting stepladder away
Lightbulb changed!

We could also run the function with different parameter values (arguments):

change_lightbulb(light_on_ceiling=False, bulb_is_hot=True)

This gives the output:

Waiting ten minutes
Lightbulb changed!

Re-use

If we had more than one lightbulb to change, with different situations, we’d just need to run the function more than once, with different parameter values:

change_lightbulb(light_on_ceiling=True, bulb_is_hot=True)
change_lightbulb(light_on_ceiling=False, bulb_is_hot=False)

This gives the output:

Getting stepladder
Waiting ten minutes
Putting stepladder away
Lightbulb changed!
Lightbulb changed!

Functions within functions

You can also call functions within other functions. Let’s say that the task of getting the stepladder was another set of several tasks, like going to the closet, pulling out the stepladder, setting up the stepladder under the light bulb, climbing the stepladder, etc. You could make another function to handle all those tasks and call it get_stepladder. You could then call it from within your change_lightbulb function like so:

def change_lightbulb(light_on_ceiling, bulb_is_hot):
    fusebox = 'off'
    if light_on_ceiling:
        print('Getting stepladder')
        # Here we call another function for the fetching stepladder tasks 
        get_stepladder()
    if bulb_is_hot:
        print('Waiting ten minutes')
        wait_ten_minutes = True
    remove_old_bulb = True
    fit_new_bulb = True
    fusebox = 'on'
    test = True
    if light_on_ceiling:
        print('Putting stepladder away')
        put_away_stepladder = True
    print('Lightbulb changed!')

You can see how you can build up code with functions in a modular way to make it easier to read, maintain and re-use.

Conclusion

As we’ve seen, functions are a neat way of grouping sets of tasks that relate to one overall task. They make it easier to add code for running a group of related tasks, and they are flexible. Functions can call other functions for even greater ease of use. They are intended as building blocks from which larger code structures are built.

Are you convinced yet, that you already know how to code? Read on in You Already Know How to Code: Part 4, or if you want to learn more, I recommend trying the free courses at CodeCademy or SoloLearn. See you next time!