Part 2: Creating Functions for an Automatic Light Application

Learn how to modularize the logic into multiple functions in our automatic light application.

We'll cover the following

Splitting the code further

In the previous lesson, we wrote code for our automatic light application. Reading this code is hard and confusing. What can we do about it? Functions are the answer, so let’s see how that works.

When breaking long code into small functions, we must remember that a function should do one thing or answer one question.

One trick we can use to identify separate tasks within the code is to read it from beginning to end, and when we feel that the application shifts its focus from one thing to another, we can insert a blank line into the code. Let’s do that for the application that we have created so far:

Get hands-on with 1200+ tech skills courses.