• 4 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 6/15/21

Write clean functions

Have you ever made origami? To make quality origami, you have to follow the instructions carefully. There’s a pattern of folds you have to follow, and you need to make sure that each one is clean, and in the right place.  If you do, your final product will be elegant.

Very nicely done

If you take a few liberties with the steps or maybe don’t press the folds down exactly in the same place as the instructions, you’ll end up with a result, but it won’t look as great.

It’s the same with writing functions.  You need to follow a set of guidelines so that what you’re creating will end up clean and usable.  Let’s look a little more closely at how to do that with the 4 steps mentioned in the video.

4 steps for writing functions

Step 1: Understand the goal

You can’t design something if you don’t know what it’s for.  It seems simple, but sometimes people get so excited about doing something, like writing a function, that they forget to pin down exactly what it's for.  That's why it's the first step!  You can formulate this in a simple sentence.

Once you're sure about the goal of your function, it’s critical to name it based on what it’s supposed to accomplish. This makes it easier to use, both for you or anyone else that happens upon it. 

Step 2: Define the end result

This is what’s going to come out of the function. In other words, what it’s going to produce. If you have a print function, then the printed document would be the end result. If your function is for making a chocolate cake, it’s going to create a chocolate cake. 🎂🍫You also need to be very explicit about the end result, because otherwise, your computer might not do what you expect.   

Computer: put in the couch, hung the pictures, added a plant...All done!
Computer: put in the couch, hung the pictures, added a plant...All done!

Step 3: Define what the function requires

In the cake function example, this would be the ingredients.  It would be something like: flour, baking powder, butter, sugar, eggs, vanilla, and so on.  It would also specify the measurements for each one.

Cake ingredients
Cake ingredients

Step 4: Implement the logic

This is when you think up all the steps for how you put the ingredients together. It seems simple, but it is actually the trickiest and most time-consuming part.  You have to take a lot of details and steps into account. Should the butter be cold, room temperature, melted? How should you incorporate the butter into the mixture?  When do you add the vanilla?

Even the actions themselves need to be details.  If you say “break the eggs,” the computer might just smash them on the floor:

Computer: All done!  I did just what you asked for!
Computer: All done! I did just what you asked for!

“Break eggs into a bowl” might lead to a bowl full of eggs and egg shells! You have to tell the computer exactly how, what, when, how many times, and so forth.

It’s worth it to take the time to do this.  After all, a function works a little like a strategic plan in American football (sorry, everyone else on the planet). 🏈 Football is run entirely on pre-planned strategies, called plays.  These plays dictate where every single player should place themselves, where they should move, and how they should interact with the opposing team. The plays are written and defined by the coach, and all the players memorize them ahead of time.

When the players huddle up, the quarterback calls out a play by name, just like a function. Based on what he calls, the players take their positions and get ready to execute what's needed for the play to be put in motion. If the play is poorly written, or the logic isn’t clear, even the best players will fail to execute it correctly.

Your functions are the same: good ones take time and energy to plan out.


  • Writing clean functions is critical for making sure they work well.

  • Good function writing takes 4 basic steps:

    • Choose a goal and name the function accordingly

    • Define the end result

    • Define what the function requires as input

    • Implement the logic

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best
Example of certificate of achievement
Example of certificate of achievement