πŸ“š Course contents

Learn Arduino, lesson 10 of 27

Arduino Functions: Write Your Own (Beginner Guide)

beginner2 min readFull course

Functions allow structuring the Program in segments of code to perform a task. The typical case for creating a function is when one needs to perform the same action multiple times in a program and keep the Code organised.

Standardizing code fragments into functions has several advantages βˆ’

  • Functions help the programmer stay organized. Often this helps to conceptualize the program.
  • Functions codify one action in one place so that the function only has to be thought about and debugged once.
  • This also reduces chances for errors in modification, if the code needs to be changed.
  • Functions make the whole sketch smaller and more compact because sections of code are reused many times.
  • They make it easier to reuse code in other programs by making it modular, and using functions often makes the code more readable.

There are two required functions in an Arduino sketch or a program i.e. setup () and loop(). Other functions must be created outside the brackets of these two functions.

int sum_func(int x, int y) {
int z = x + y;
return z;
}
void loop() {
β–Ά int result = sum_func(5, 6);
Serial.println(result);
}
Variables
result?

loop() calls sum_func with 5 and 6.

Arduino Functions: Write Your Own (Beginner Guide)