void setup() {
Serial.begin(9600);
pinMode(9,OUTPUT);
}
void loop() {
blinkLEDFast(); //calling the blinkLEDFast function
blinkLEDSlow(); //calling the blinkLEDSlow function
}
void blinkLEDFast(){ //declaring a function to blink led's fast
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(100);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(100);
}
void blinkLEDSlow(){ //declaring a function to blink led's slowly
digitalWrite(9,HIGH);
delay(500);
digitalWrite(9,LOW);
delay(500);
digitalWrite(9,HIGH);
delay(500);
digitalWrite(9,LOW);
delay(500);
}
What is a function? How to declare and call functions?
A function declaration is as follows and happens after the void loop. If the function does not return a value it is declared with void type. If it returns a value then it has the variable type of the returned value, in this case int.
or
To call a function simply use the name of the function in a line of code. A function may or may not have parameters (inputs to the function). Above is an example of two functions without input parameters, below is an example with two input parameters l and w. The values of 5 and 6 are then passed into the function when it is called. You must call you functions within the loop.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("The area is ");
Serial.println(areaFunction(5,6));
}
int areaFunction (int w, int l){
int area = l * w;
return area;
}