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 creating the library, that is stored in a separate file. This is how external libraries are created.

Functions

Functions are the set of statements that are executed always 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 is usually trying to make several functions that contain all the statements and then call 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);	
}

In the example above can be seen that the return type of aexampleFunction function is void that 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 loop()
  int result = sumOfTwoNumbers(2, 3); 
}

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.1687713055.txt.gz · Last modified: 2023/06/25 14:10 (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