Last Updated: 28/07/2021

Lesson 9 - Functions

Lessons >> Basics >> Lesson 9 - Functions

Lesson Contents

Basics of creating and using functions
Function syntax
Calling a function
Understanding a void function
Understanding a function that returns an int
Passing a value to a function
Passing a value to a void function
Understanding variables declared within a function
Calling a function multiple times
Dangers of sending an overrun integer to a function

Example 1: functionsv1.ino


Click to Download code:functionsv1.ino

Basic void function
Parts of a function
Calling a function

 
/* 28/07/2021
   functionsv1

   A simple void function

*/

int q;
int myAnswer;

//void means we are not expecting a return value
void firstFunction(){
  //simple for loop
  for (q = 0; q < 10; q++) {
      myAnswer = q + 3 - 10 + 22;
      Serial.print("myAnswer = ");
      Serial.println(myAnswer);
      delay(100);
    }  
}

void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("functionsv1...");
  //this will print a blank line
  Serial.println(" ");

}

void loop() {
  //if statement only valid while q is smaller than 1
  if (q < 1) {
    /*
    //simple for loop
    for (q = 0; q < 10; q++) {
      myAnswer = q + 3 - 10 + 22;
      Serial.print("myAnswer = ");
      Serial.println(myAnswer);
      delay(100);
    }
    */
    firstFunction();
  }
}

Example 2: functionsv2.ino


Click to Download code:functionsv2.ino

Sending a value to a function
Returing a value from a function
Variables declared within a function

 
/* 28/07/2021
   functionsv2

   Sending a value to a function and returning a value from a function.

*/

int q;
int myAnswer;


// int means it MUST return an int value
int intReturnFunction(int incomingValue){
  int myReturnValue;
  myReturnValue = incomingValue + 3 - 10 + 22 + 100;
  return myReturnValue;
}

void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("functionsv2...");
  //this will print a blank line
  Serial.println(" ");

}

void loop() {
  //if statement only valid while q is smaller than 1
  if (q < 1) {
    //simple for loop
    for (q = 0; q < 10; q++) {
      //myAnswer = q + 3 - 10 + 22;
      myAnswer = intReturnFunction(q);
      Serial.print("myAnswer = ");
      Serial.println(myAnswer);
      delay(100);
    }
  }
}

Example 3: functionsv3.ino


Click to Download code:functionsv3.ino

Calling multiple functions
Sending values to a void function
Using a function multiple times
Dangers of over running a variables limits in a function

 
/* 28/07/2021
   functionsv3

   send a value to a void function

*/

int q;
int myAnswer;


// int means it MUST return an int value
int intReturnFunction(int incomingValue) {
  int myReturnValue;
  myReturnValue = incomingValue + 3 - 10 + 22 + 100;
  return myReturnValue;
}

//void function with an int being sent to it
void printAnswer(int answerToPrint) {
  Serial.print("my function printed Answer = ");
  Serial.println(answerToPrint);
}

void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("functionsv3...");
  //this will print a blank line
  Serial.println(" ");

}

void loop() {
  //if statement only valid while q is smaller than 1
  if (q < 1) {
    //simple for loop
    for (q = 0; q < 10; q++) {
      //myAnswer = q + 3 - 10 + 22;
      myAnswer = intReturnFunction(q);
      printAnswer(myAnswer);
      printAnswer(11111);
      printAnswer(22222);
      printAnswer(30333);
      //This line exceeds the value of an int so over runs and gives a negative value
      //Danger of sending an invalid value to a function.
      printAnswer(33333);


      
      /*
        Serial.print("myAnswer = ");
        Serial.println(myAnswer);
      */
      delay(100);
    }
  }
}

Additional Resource Links

Don't forget to use the Reference in your Arduino IDE and look at Functions for more help on this lesson.

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Lesson 9 - Functions as a reference.