Defining new functions in Arduino

Defining new functions in Arduino is equivalent to defining the functions in C.

The syntax is −


return_type function_name(arg_type arg)

The only difference is that in C, the function needs to be declared at the top, if it is invoked before its definition. No such constraint exists in Arduino. The following code demonstrates this −


void setup() {
void loop() {
   // put your main code here, to run repeatedly:
   for (int i = 0; i < 10; i++) {
      long int w = square(i);
long int square(int a) {
   return (a * a);

The Serial Monitor output is shown below −


As you can see, even though the function was invoked before being defined, Arduino did not throw up an error. The execution worked just as expected.