Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $776

Donations Received

Top Donor: C.C. $100

X

Returning a Value from a Function

Created on: 6 March 2015

Part 16 of the Arduino Programming Course

In the previous part of this Arduino programming course, we looked at how to pass a value to a function. Now we look at how to get a value back from a function.

Getting a value back from a function is called "returning" the value from the function. The return keyword is used at the end of the function to get the value back. We must also say what type of value the function is returning, e.g. int, float, etc.

The example sketch below uses a function to do a mathematical calculation and then return the result of the calculation which can then be used in the main Arduino sketch.

Function that Returns a Value

void setup() {
    float area;
  
    Serial.begin(9600);
    // calculate the area of a circle with radius of 9.2
    area = CircleArea(9.2);
    Serial.print("Area of circle is: ");
    // print area to 4 decimal places
    Serial.println(area, 4);
}

void loop() {
}

// calculate the area of a circle
float CircleArea(float radius)
{
    float result;
    
    result = 3.141592654 * radius * radius;
    
    return result;
}

What the Sketch Does

The sketch calculates the the area of a circle from a radius value of the circle that is hard-coded into the sketch – in the example sketch the value is set to 9.2, but you can set it to any value that you want. The result of the calculation is then sent out of the serial port so that it can be seen in the Arduino IDE Serial Monitor window.

The formula for calculating the area of a circle is:

A = π × r²
OR
A = π × r × r
Where:
A = area of the circle
π = PI = 3.141592654
r = radius of the circle

In other words, if we know the radius of the circle (radius is the distance from the centre of the circle to the edge) we can calculate the area of the circle.

The unit that the radius is in can be any unit that is used to measure distance and the area will be squares of the unit used, e.g. if the radius is in centimetres, the area will be in square centimetres, if the radius is in feet, the result will be in square feet.

The video below shows the the sketch running.

Can't see the video? View on YouTube →

Amazon.com

Amazon.co.uk

How the Sketch Works

The CircleArea() function must return a value, so is preceded by the type of value that it must return – in this case float. A float value called radius is also passed to the function as explained in the previous part of this course.

float CircleArea(float radius)

Inside the function body, the radius calculation is done and the result of the calculation is put into the variable result which is a variable created in the function.

The function then returns the result using the return keyword at the bottom of the function.

return result;

The formula is translated into code for the Arduino as follows:

A = π × r × r
Becomes:
result = 3.141592654 * radius * radius;

In the part of the sketch that calls the CircleArea() function, the function basically becomes the value that it returns and can be assigned to a variable.

The variable area is assigned the value that the CircleArea() function returns:

area = CircleArea(9.2);

After this, the result of the calculation, which is the area of the circle, is sent out the serial port to be displayed in the Arduino IDE Serial Monitor window.





A Shorter Version of the Sketch

The sketch above can be written in a shorter way without using some of the intermediate variables as shown below.

void setup() {
    Serial.begin(9600);
    Serial.print("Area of circle is: ");
    // print area to 4 decimal places
    Serial.println(CircleArea(9.2), 4);
}

void loop() {
}

// calculate the area of a circle
float CircleArea(float radius)
{
    return (3.141592654 * radius * radius);
}

In this sketch, the CircleArea() function returns the result of the calculation on one line without first assigning it to a variable.

return (3.141592654 * radius * radius);

This method of doing the calculation and returning the value is fine, although it may not be as easy to read the code as the first example.

When the CircleArea() function is called in the sketch, it is passed to Serial.println() as if it were a variable. This is possible because when a function returns a variable, it takes on the value of the variable. The sketch therefore works the same way as the first sketch, although again, it is more difficult to read the code.

Serial.println(CircleArea(9.2), 4);

The size of the binary output file (the file that gets loaded to the Arduino after compiling) from the Arduino compiler is 4,040 bytes for both sketches in Arduino IDE version 1.0.6.

← Go back to Part 15Go to Part 17 →