Search⌘ K
AI Features

Part 3: Creating Functions for an Automatic Light Application

Explore how to create functions that return values and simplify program flow in an automatic light application. Understand the benefits of modular code by breaking complex tasks into functions for better readability and maintenance.

We'll cover the following...

Returning values from a function

We have previously created a function, but now we must check whether this function needs to return something. It calculates the distance, which we need to be outside of the function—so that value needs to be returned.

Again, we have two options. We can either return the value contained inside the variable distance like this:

C++
distance = 2 * earth_radius * asin(sqrt(a))
return distance

Another option is to make this shorter, because in the previous example, the distance variable is being used to hold the distance for one line. So, instead of ...