Why no semicolon after the two mandatory functions?

I’m using more functions. They are all written without a semicolon, e.g, void myFunction () and then used with one, e.g. myFunction();.

I’ve only just become aware that the two mandatory functions, setup and loop, don’t follow this rule, and I’m wondering why not?

Please post your entire program using a code block </>

What rule?

or… yes they do.

What you might never have seen is where loop() and setup() are called.

a7

First is a declaration at the start of the function and incomplete, the second is how it's invoked.

:smiley_cat:

Read about forward declaration

A forward declaration requires the semicolon.

:smiley_cat:

Like this (in arduino.h):

void setup(void);
void loop(void);

Appreciate all replies. I’ve done some initial reading about ‘forward declarations’. But so far come across no explanation why neither setup() nor loop() have a semicolon at their ends?

These are both actual functions, not declarations, yes? So…?

The declaration is in 'arduino.h', see reply #8. The implementation is in your sketch, whatever that happens to currently be.

Hint: ';' is short form for '{ }'

Post the code where you see this.

But, no functions require (nor should have) a semicolon after their implementation.

BTW, there's no such terminology as a "mandatory function". setup() and loop() are just functions.

@Terrypin just learn it and believe it or… strap in and hit google:

a7

I thought the best way to describe a semicolon is the end of a 'statement'. A brace defines scope.

Since a brace defines the scope, when scope ends, it ends the statement. The semi colon is optional.

if (x) {
    int a;
    .
   {
     int a;
      . 
      .
   }
} else....

the inner bracket group specified the scope of it's 'a' variable...


I think the history of C may explain how these variations occurred... My first language was 'B', showing my age, and the semicolon was a required part of the statement. B was a 'typeless' language in that it was the machines 'word' size for everything.

B came about from the BCPL developed from Multics by Ken Thompson...I think.. it's been a year or two since I thought about it....

Here is a document from Dennis M. Ritchie, about the history of C and you can see how things were implemented and changed... I think he might know a little about what's happening back then...

https://www.bell-labs.com/usr/dmr/www/chist.html

:smiley_cat:

1 Like

Not so much optional as being taken as an empty statement. You can strew semicolons around quite a bit.

The superfluous one I like is at large between functions.

a7

When I write the code for setup() and loop() functions in my sketches, they have curly braces with no semicolon just like any other function in my code.

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

}

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

void myFunction() {
  Serial.println("In myFunction()");
}

setup(), loop(), and myFunction() all have the same form.

That's the function definition, to use (call) the function:

if(x > y)
  myFunction(); //<<<<< semicolon

I know how to call it. If you look at my loop() implementation then you will see that I it contained such a call.

My reply was meant for the OP, @Terrypin, where he indicated that the implementation of his own functions have a different form than setup() and loop(). I was just pointing out that all of my function definitions have the same form. No matter if they are my own custom functions or the ones that Arduino requires.

Thanks, that and further links helped me to reach the simple solution I sought. Please correct me if my following conclusion is wrong:

In Arduino programming the two mandatory functions, setup() and loop(), are special defaults, thankfully inbuilt within the IDE. I assume they are presented in that inconsistent way for simplicity and immediate ease of use, awaiting user declaration.

This post by Ayush Budhiraja (in a C/C++ forum) got the penny dropped for me:
https://www.quora.com/Why-do-we-not-use-a-semicolon-after-the-main-function-in-C-and-C++

I assume that the subject functions are treated similarly to main() in C.

It appears in every newly started sketch.

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

"BTW, there's no such terminology as a "mandatory function"

Nothing to do with 'terminology'. It's just a simple fact that all Arduino sketches must have two functions: setup() and loop(). That's what 'mandatory' means!

Thanks, got it, see my post #18.

Maybe I've misunderstood, but I don't get that.
For example, this compiles:

void setup()
{
}

void loop()
{
}

but this does not:

void setup()
{
}

void loop();

It reports:
undefined reference to `loop'

I thought perhaps you meant this?
Hint: ';' is short form for '()'
but that gives the error
variable or field 'loop' declared void