Nope
You are understand nothing from others answers...
that suggests a function that does nothing.
i've often written stub functions -- void func (void) { }
the closing brace of a function unambiguously completes the function definition. no need for a ";"
Ken Thompson and Rob Pike went to google and helped develop the Go Progamming language which they felt was a cleaner implementation that is more pure OO. they got rid of the ";"
in some cases, a struct definition can close at the end of the brace or variable of its type can be defined. the ";" avoids this ambiguity.
an array can be initialized with values within a brace that requires a ";". a ";" is required at the end of the variable definition, therefore explaining the need
because it's not defined
below has both a declaration and definition
void setup()
{
}
void loop();
void loop () {}
many programs are composed of multiple files. when a function defined in one file is invoked in another, the compiler needs to see a declaration of the function in the file invoking it to know what the arguments to the function are as well as the return value.
a separate file can define avg
float
avg (
int vals [],
int nVals )
{
float sum = 0;
for (int n = 0; n < nVals; n++)
sum += vals [n];
return sum / nVals;
}
a different file can invoke avg
float avg (int vals [], int nVals);
int var [] = { 1, 2, 3, 5, 7, 9 };
int
main ()
{
printf (" %.2f\n", avg (var, 6));
return 0;
}
Thanks, I do too. But the subject mandatory functions are present without either an included library or a declaration.
Do you agree with the explanation I've worded in my post #18?
they are simply external function invoked within main.cpp (below)
declared in Arduino.h as
void setup(void);
void loop(void);
/*
main.cpp - Main loop for Arduino sketches
Copyright (c) 2005-2013 Arduino Team. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
void setupUSB() __attribute__((weak));
void setupUSB() { }
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
That prompts another question: does that mean the following is unnecessary in the many sketches I’ve seen here, and use myself?
# include “Arduino.h”
Yes it does mean that. But it is often used legitimately in library code, where it's not included by default.
Understood. Interesting that DFRobot’s own examples for its DFR MP3 Mini Player include that in its opening statements. And, perhaps understandably, perpetuated in hundreds of subsequent user examples.
It's redundant (but harmless) when using only .ino files in the Arduino IDE. However, if you use a different IDE (for better features), or write code using .h / .cpp files then it's required.
@Terrypin I am not convinced that you have "got" it
In C++ all functions need a function prototype that tells the compiler that the function exists, its name, the parameters that the function takes and its return data type.
These prototypes require a semicolon at the end but contain no code. The prototype must appear in the code before the actual function definition and before the function is called
So a function prototype would look like this
int addTwo(int x, int y); //return type, function name, parameters, semicolon
To keep things simple the Arduino will create these function prototypes for you and silently compile them into the code if you do not provide them, so you will never see them
When you call a function the call needs to be terminated with a semicolon but the call does not need a return type so the call would look like this
int sumOfTwo = addTwo(2, 3); //function name, parameters, semicolon
The actual function definition would look like this
int addTwo(int a, int b) //return type, function name, parameters, no semicolon
{
return a + B;
}
As has been explained, the function prototypes for loop() and setup() are in Arduino.h which the compiler will #include in the sketch without you needing to #include it
No prototype is required if the function is implemented (defined) before it used ... iff everything is in a single file.
Point taken, but for such a simple concept it is surprisingly difficult to describe all of the possible combinations of function declaration, definition and use
The fact that the Arduino IDE creates the function prototypes, whilst helpful, can also confuse things. It gets particularly confusing when a function has optional parameters so I left that out of the explanation as well
To throw some mud in the water, that line is a declaration of an integer variable initialised by a call to a function.
A high percentage of calls will not be in the context of a declaration.
int someInt; /// declared somewhere, global, local, whatevs
.
.
.
someInt = addTwo(2, 3); // call the function and assign the return(ed) int
It is hard to explain things when you don't remember not knowing them. ![]()
a7
A forward declaration tells the compiler that somewhere in the source code is a function with this name, that takes these parameters, and returns the following. That is all the compiler needs to set up a call to that function. Any time the compiler sees that function in the code, it knows what parameters need to be set up, and what to expect back. The actual assembly code for a function call is to evaluate the parameters, and push them onto the stack, then do a CALL instruction to a label. When the call returns, the assembly code cleans up the stack, and then does something with the returned value.
At some point though you have to tell the compiler how to implement that function. To do that, you have the same function name, return type, and parameter list, but this time you follow that with a block of code, marked with { and }, that actually does whatever the function is supposed to do. The compiler will create the assembler code to do the function, and assign a label to the entry point for that code. That label will be the same as the one used in the CALL instructions where the function is called.
In the Arduino IDE, the declaration of the setup and loop functions are in the header file Arduino.h In there, they include the semicolon at the end, as they are merely telling the compiler that those functions exist. In your code, you are telling the compiler how to do those functions, so the semicolon gets replaced with a block of code telling the compiler how to perform a 'setup' or a 'loop'
True, but it was the syntax of the call to the function that I was trying to illustrate
Amen to that
I stand by my summary in post #18. You seem to have extended the discussion far beyond my simple question about a syntax inconsistency. I may well need to learn more about those points you and others raise at some future stage, but for now I’m quite content thanks!
But it is wrong
There is nothing special about the setup() and loop() functions
The summary in #18 is wrong.
There is nothing "special" in arduino loop() and setup(), they are normal C++ functions
Kinda maybe no not really.
They are in place ready to go simply because they are in your default new sketch… they are not necessary, they are not mandatory, they are only strongly suggested and nearly universally used.
I got tired of adding the few things I always add and deleting the few things I always delete
// code here runs once
// code here runs over and over
Or however they commented on setup() and loop(), no duh! Not my first sketch!
I laugh when I see code here that still has those helpful noob comments riding around in it.
a7