Intermediate 10: Advanced Functions
We continue our lessons with an expansion on functions.  



Functions are an integral part of any coding language.  In previous lessons, we introduced our knowledge of functions by learning about void functions and how to write them.  Now we will complete our understand by learning about return functions, nested functions, function overloading, and more.


Return Functions

A return function is a function that gives you a value.


return data type FunctionName(inputs) {

return ReturnValue;

return data type – This is where you declare the data type of your return value.

Function Name – The name of the function.

inputs – The input parameters that you will use in the function.

statements – The code that the function will run when called.

return – A special command to return the indicated variable value as the function’s output.


Instead of using void  for our return data type, a return function requires that you initialize the data type of the return value.



Return functions use the special keyword return .  This command instantly exits out of the function and returns the indicated value as the function’s output.


Writing a Return Function

Let’s start with some examples of simple return functions.


Example 1: Multiply Function

This function will accept two integer inputs and multiply them together.  It will then return the answer.

Simple enough, right?

the special keyword return  indicated that the function will end and the value of the specified variable will be the returned value.


Example 2: Multiple Return Options

Though it’s not possible to have more than one return value, it is possible to allow more than one return option.

For example, this function will return 1 if the input number is less than 10 and 0 otherwise.


Notice we have two return possibilities, but only one of them can be true at a time.

Using return instantly exits out of the function, so if multiple returns are arranged in the same scope, only the first one will execute.


Example 3: Simplified Joystick Inputs

The current CoDrone Joystick Input code looks like this:

You can see that there is a lot of repetition here.

We can simplify this by creating the function JoystickRead

and rewriting it


Calling Return Functions

Technically, you can call a return function exactly how you would call a void function:

However, doing it this way will not store the returned value for JoystickRead() .

Instead, we treat return functions just like we would a raw value.

This calls the function and saves the return value as the integer variable x .


Example 4: Return Call

Here we demonstrate that return functions can also utilize mathematical operators.  We subtract 10 from the returned value of JoystickRead() .


Nested Functions

In programming, the term ‘nested’ just describes when a loop is used within a loop, or a function is used within a function.

For example,

shows a nested for-loop.

In the example we used above

the function CoDrone.AnalogScalechange()  is nested within JoystickRead() .


Function Overloading

The C++ language allows function overloading.   This allows you to declare a function multiple times with different numbers of inputs.

Example 5: Overloaded JoystickRead()

The value for x , y , and z  would all differ here.

int x = JoystickRead();   returns 0.

int y = JoystickRead(23);  returns the value of Value  for pin 23.

int z = JoystickRead(23,2);  returns the value of Value  for pin 23 multiplied by 2.


Advanced Planned Flight

We’ll use what we’ve learned in this lesson to improve our planned flight code from last lesson.

Improved Code

We add 3 new functions:

an overloaded version,


There’s a lot of ways we could simplify this code even more with functions if we wanted to.  One advantage of using a lot of functions is how it organizes our programs.

The more functions we use, the shorter and more concise our main code becomes.


We can also now easily add standard flight controls to our planned flight code by simply adding SetMove()  to the end of   void loop() .

Final Code with Flight Controls