Arduino Basic Tutorial

How to Create User Defined Function in Arduino


Function in Arduino- in this article, I am going to show you how to create functions in Arduino and how to use them in your project.

User-Defined Function in Arduino:

Let’s imagine that we have a set of instructions that we have to write repeatedly in different parts of our sketch. Wouldn’t there be some way to be able to invoke that set of instructions by means of a simple name without having to retype all of them each time? Yes, through the creation of functions. A function is a piece of code that is identified with a Name. In this way, all the code included within it can be executed simply by writing your function name in the desired place in our sketch.

By creating our own functions in Arduino, we write much more readable code and are easy to maintain. Segmenting the code into different Arduino functions allows the programmer to create modular pieces of code that perform a defined task. In addition, a function in Arduino can be reused in another sketch, so that with time we can have a very complete collection of functions in Arduino that allow us to write code very quickly and efficiently.

In short, including code snippets in functions has several Advantages: functions in Arduino help the programmer to be organized (often this helps to conceptualize the program), they encode an action in a place, so that a function only has to be thought and written once (this also reduces the chances of errors in a modification, if the code has to be changed) and make it easier to reuse code in other programs (causing these are smaller, more modular and legible).

To create our own functions in Arduino, we must “declare” them. This is done in anywhere outside of “void setup()” and “void loop()” – thus either before or after both sections -, following the syntax set by the template following:

Syntax of user-defined function in Arduino


  • “ReturnType” is one of the well-known types (“byte”, “int”, “float”, etc.) and indicates the type of value that the function will return to the main sketch once executed. This return value can be saved in a variable to be used in the main sketch, or it can just be ignored. If I don’t know you want to return no data (i.e .: let the function perform its task period), a special one called “void” can be used as a return type or not specify none. To return the data, you must use the instruction return value ;, which has as a “collateral” effect the end of the execution of the function in Arduino. This usually makes the “return” statement the last in be written inside the function code. If the functions in Arduino return nothing (is that is, if the return type is “void”), it is not necessary to write it.
  • “Type param1, type param2, …” are the declarations of the parameters of the function, which are nothing more than internal variables whose existence only lasts as long as its code is running. The number of parameters it can be any: none, one, two, etc. The initial value of these parameters is explicitly assigned in the “call” to the functions in Arduino (that is, when it is invoked within the main sketch), but this value can vary within your internal code. In any case, at the end of the execution of the functions in Arduino, all its parameters are destroyed from the memory of the microcontroller.

Examples of Function in Arduino:

how to create a calculator using user-defined functions in Arduino:

Let’s see an example of creating a calculator using user-defined a function that returns the result of multiplication, addition, subtraction, and division of two numbers passed as parameters:


Function in arduino

Function with global variables in Arduino programming:

One might think that using parameters in a function is not necessary because by means of global variables any function could access the values needed to work. That is, we could have done something like this:


Function in arduino

In the above code, both “first number” and “second number” have been declared as global variables, and the multiply() function has not been needed by both no parameters. However, the problem of using global variables instead of passing parameters to the function is the loss of elegance and flexibility. What would happen if in addition to wanting to multiply 2×3 we wanted to multiply 4×7? If using parameters, it would be as simple as passing the new values ​​directly as parameters (like this: multiply (4,7);) to get what we want. Instead, to use global variables we would need to change their values ​​every time we would like to multiply different numbers (or have several global variables to each number that we wish to multiply, something quite unfeasible), with which we could alter the behavior of other parts of our code and be a source of errors.

Finally, indicate that although until now we have been calling “Instruction” to the different commands of the Arduino language (some within objects and not others), in fact, all these instructions are also functions in Arduino. Functions that allow you to invoke a set of code written in invisible C language for us. Even what we have been calling the “section” “void setup()” and the “Section” “void loop()” are also functions in Arduino (without parameters and without return value, as you can see).

how to print a message on the serial monitor using a user-defined function in Arduino:


Function in arduino

Reading sensor data in Arduino function:

Circuit diagram:

Function in arduino

Here I used a potentiometer as a sensor. The circuit diagram is very simple as you can see I connected the Arduino 5v with the right leg of the potentiometer and Arduino ground with the left leg of the potentiometer and the middle leg of the potentiometer is connected with Arduino analog pin A0.

Example: how to read analog sensor data using a user-defined function in Arduino


Function in arduino

Function in arduino


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button