-1

I have noticed the following code, which is obviously invalid C++, compiles in Arduino IDE (using AVR-GCC):

// The program compiles even when the following
// line is commented.
// void someRandomFunction();


void setup() {
  // put your setup code here, to run once:
  someRandomFunction();
}

void loop() {
  // put your main code here, to run repeatedly:

}

void someRandomFunction() {
}

What is going on here? C++ requires functions to be declared before they are used. When the compiler comes to the line someRandomFunction() in the setup() function, how does it know it will be declared?

FlatAssembler
  • 667
  • 7
  • 30
  • 2
    Dunno. May be you are compiling as C code? – Adrian Mole Jan 12 '21 at 21:05
  • 2
    Arduino C++ more a dialect of C++, then actually "C++". With that said though, [I cannot reproduce](https://godbolt.org/z/Wand1T). Which version are you using? What compiler options? – NathanOliver Jan 12 '21 at 21:15
  • 2
    To add to @AdrianMole's comment, what's the name of the file in which this program is placed? [C accepts it with warnings](https://godbolt.org/z/axoM7a) – Ted Lyngmo Jan 12 '21 at 21:15
  • 1
    Arduino "sketches" and the many libraries are largely a horribly bloated and dumbed-down C++ variant suitable only for tinkerers and students who eschew reading and learning, preferring only to copy and paste. If you want to create real software for most of these embedded devices, ditch the .INO and write in assembly or C. Avoid dynamic allocation. – TomServo Jan 12 '21 at 23:06
  • 1
    As @Shuyna alludes, there is a lot going on behind the scenes. Your `*ino` file gets pre-processed, and function declarations are created automatically for you. This is why we don't have to declare functions before use. It also calls the standard `avr-gcc` to compile your program, link with libraries, and create the hex files for upload. All from one click. In preferences, turn on verbose output during compiling, and you'll see everything going on. Some folks have a real hatred for it; for me it's a simple tool to get things done. (writing C since 1981). – aMike Jan 13 '21 at 01:50

2 Answers2

2

This is what we call Forward declaration and in C++ it only requires you to declare the prototype of the function before attempting to use it, instead of defining the whole function.:

Taking as example the following two pieces of code:

CODE A:

#include <Arduino.h>
void setup(){}
void AA(){
  // any code here
}
void loop(){
  AA();
}

CODE B:

#include <Arduino.h>
void setup(){}
void loop(){
  BB();
}
void BB(){
  // any code here
}

Strictly speaking C requires that functions be forward declared for the compiler to compile and link them. So in CODE A we do not have declared the function but it defined, which makes it legal for proper C code. But the CODE B has the function definition after the loop, which would be illegal for plain C. A solution would be the following one:

#include <Arduino.h>

void BB();

void setup(){}
void loop(){
  BB();
}
void BB(){
  // any code here
}

This, however, to fit the Arduino script format of having a void setup() following from a void loop(), required Arduino to include a script on its IDE that automatically looks for functions and generate prototypes up top for you so you do not need to worry about it. So despite being written in C++, you will NOT see Arduino sketches using Forward declaration often in their sketches, as it works out fine and it is often easier to read having first setup() and loop().

Shunya
  • 2,344
  • 4
  • 16
  • 28
2

Your file is .ino not .cpp. .ino is 'Arduino language'.

The main ino file with additional ino files are processed by the Arduino builder to C++ source and only then processed as C++ (preprocessor, compilation).

Juraj
  • 3,490
  • 4
  • 18
  • 25