This is an old revision of the document!


Sub-programs, Functions

In many cases, the program grows to a size that becomes hardly manageable as a single unit. It is quite difficult to navigate through the code that occupies many screens. In such a situation, subprograms can help. Subprograms are named functions in C, and in C++, while they are associated with an object, they are named methods (in this chapter, the name function will be used). The function contains a set of statements that usually form some logical part of the code that can be separately tested and verified, making the whole program easy to manage. It is possible to group many functions by creating a library that is stored in a separate file. This is how external libraries are created.

Functions

Functions are the set of statements that are always executed when the function is called. A function can accept arguments as its input data and return the resulting value. Two functions from the Arduino programming model that were mentioned before are already known – setup() and loop(). The programmer usually tries to make several functions containing all the statements and then calls them in the setup() or loop() functions.

The structure of the function is following:

type functionName(arguments) //A return type, name, and arguments of the function
{
  //The body of a function – statements to execute
}

For example, a function that periodically turns on and off the LED can look like this:

void exampleFunction() 
{
  digitalWrite(13, HIGH); //the LED is ON		
  delay(1000);			
  digitalWrite(13, LOW);  //the LED is OFF				
  delay(1000);	
}

The example above shows that the return type of aexampleFunction function is void, which means the function does not return any value. This function also does not have any arguments because the brackets are empty.

This function should be called inside the loop() function in the following way:

void loop()
{
  exampleFunction(); //the call of the defined function inside loop()
}

The whole code in the Arduino environment looks like this:

void loop()
{
  exampleFunction(); //the call of the defined function inside loop()
}
 
void exampleFunction() 
{
  digitalWrite(13, HIGH); //the LED is ON		
  delay(1000);			
  digitalWrite(13, LOW);  //the LED is OFF				
  delay(1000);	
}

It can be seen that the function is defined outside the loop() or setup() functions.

When some specific result must be returned as a result of a function, then the function return type should be indicated, for example:

//the return type is "int"
int  sumOfTwoNumbers(int x, int y) 
{
    //the value next to the "return" should have the "int" type; 
    //this is what will be returned as a result.
    return (x+y); 
}

In the loop(), this function would be called in the following way:

void loop()
{
  //the call of the defined function inside the loop()
  int result = sumOfTwoNumbers(2, 3); 
}

Built-in functions

Every programming SDK, including Arduino IDE, comes with several ready-made functions that help develop applications, reducing the effort and time of writing programs significantly. These functions are written to handle inputs and outputs, process texts, communicate using serial ports, manipulate bits and bytes, and perform mathematical calculations. Refer to Arduino or other SDK documentation for details.

Library functions

The popularity of microcontrollers and embedded programming caused the growth of communities of enthusiasts who create a vast of useful software. This usually comes in the form of a set of functions created to handle some specific tasks, e.g. interfacing with a family of graphical displays or communicating using the chosen protocol. Functions created for one purpose are grouped together, forming the library. The number of libraries and their different version is so big that software developers use a special library manager to ensure that libraries are up-to-date or keep them in stable versions.

Check Yourself

1. What are the built-in functions used for?

  • To reduce the size of the program.
  • To delete unnecessary functions.
  • To simplify the source file.
  • To increase the speed of the program.

2. Which of the following statements is true?

  • Built-in functions must return a value.
  • Built-in functions cannot return values.
  • The compiler can ignore the declaration of the built-in function.
  • Built-in functions cannot contain more than 10 lines of code.

3. Is it possible to guarantee that the declared built-in function is really built-in?

  • Guarantee is not possible; in each case, it is different.
  • Can be confidently ensured that the function you have declared as built-in is really built-in.
en/iot-open/introductiontoembeddedprogramming2/cppfundamentals/functions.1688768672.txt.gz · Last modified: 2023/07/07 19:24 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0