This is my first posing in this forum. I'm learning to handle Arduino or better the Arduino C/C++ language. Right now, I try to understand why I'm getting an error if I run a function outside another function.
Variables can be declared outside / before functions? And all other code will have to (or better must) be inside the setup() or loop() function to run? I didn't notice that...
Variables can be declared either outside of a function, in which case they will have global scope and be available throughout the program, or inside a function in which case they will have local scope and will only be available in the function. Running the function again will declare a new variable of the same name and its previous value will not be retained unless it is declared static
Ok, I think I understand the scope-concept. I think I'm too "newbie" to understand this fundamental OOP.
When I'm declaring a "StringObject" globally, why can't I use a "StringObject Function" on this globally declared Object globally? Why can I only use it in the scope of another function?
ReplaceError_String_sketch_feb01a:2:1: error: 'myString' does not name a type
myString.replace("World", "Harry");
^~~~~~~~
exit status 1
'myString' does not name a type
Ok, I read that. Variable declaration is also code in my opinion. But obviously, I can't do anything else than define constants, variables, and load headers.
Thanks a lot! :-*
This is not a function declaration.
This is a call to the String class method.
C(++) is a procedural language. Code executes one line at a time, starting at
int main(void) {
found deep inside the ArduinoCore-avr, part of the magic behind Arduino, which calls setup() first and then loop() repeatedly indefinitely. Instantiating a String object happens outside of this procedure, and only exists in source code. You declare, instantiate, initialize myString with the line
String myString = "Hello World!\n";
You can do this "outside" setup() or loop() in which case it is a global object, residing in static RAM and having global scope. If you do this inside setup() or loop(), it is a local object, appearing on the stack in managed RAM for the duration of execution of that function, and having a local scope, meaning it can only be referenced inside that function. As a class of String, myString inherits, as I mentioned, the replace() method. Methods, just like functions, ARE procedural, so that line of code MUST exist within the procedural body, meaning it can exist inside main() or one of the functions that are called from main() or one of the functions called by those functions or one of the functions called by those functions... But, it can only exist inside a function that has scope of the object, so if myString is declared in setup(), replace() cannot be called from loop().
This is a trivial example of course, but there are some circumstances where you might need complex initialization of global variables (or const local variables). You can use an immediately invoked lambda expression:
[]{ ... } defines an inline (lambda) function that takes no arguments, and by adding () you invoke/call that function.
The code above is equivalent to the following, but it's shorter, and it often doesn't make sense to add a named function that you're just going to use once, you want the logic of the initialization in the same place in your code as the variable you're initializing.
He is a newbie- don't confuse him with Lambda functions- which I suspect the very few of us use them.
On the other hand, Henry- welcome to the forum.
Since you are just learning, do yourself a favor and do not use Strings. As you learn more and use Arduinos more, the evil String will bite you. Strings waste RAM and if used enough in a program, can use up memory and cause a crash that's practically impossible to identify.