# Why no semicolon after the two mandatory functions?

**URL:** <https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679>\
**Category:** Programming\
**Created:** [October 16, 2022, 10:02pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679 "2022-10-16T22:02:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Terrypin](https://avatars.discourse-cdn.com/v4/letter/t/a3d4f5/32.png) [@Terrypin](https://forum.arduino.cc/u/Terrypin)\
**Post date:** [October 16, 2022, 10:02pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/1 "2022-10-16T22:02:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [October 16, 2022, 10:10pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/2 "2022-10-16T22:10:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon44338819](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anon44338819](https://forum.arduino.cc/u/anon44338819)\
**Post date:** [October 16, 2022, 10:14pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/3 "2022-10-16T22:14:51Z")

</div>

> [@Terrypin](#):
>
> don’t follow this rule

_What_ rule?

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 16, 2022, 10:16pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/4 "2022-10-16T22:16:22Z")

</div>

> [@Terrypin](#):
>
> don’t follow this rule

or… yes they do.

What you might never have seen is where _loop_() and _setup_() are _called_.

a7

---

<div class="post-metadata">

**Author:** ![jkwilborn](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@jkwilborn](https://forum.arduino.cc/u/jkwilborn)\
**Post date:** [October 16, 2022, 10:20pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/5 "2022-10-16T22:20:02Z")

</div>

> [@Terrypin](#):
>
> **void myFunction ()** and then used with one, e.g. **myFunction();**.

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

😺

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 16, 2022, 10:25pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/6 "2022-10-16T22:25:05Z")

</div>

Read about forward declaration

---

<div class="post-metadata">

**Author:** ![jkwilborn](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@jkwilborn](https://forum.arduino.cc/u/jkwilborn)\
**Post date:** [October 16, 2022, 10:25pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/7 "2022-10-16T22:25:58Z")

</div>

A forward declaration requires the semicolon.

😺

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [October 16, 2022, 10:33pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/8 "2022-10-16T22:33:09Z")

</div>

Like this (in arduino.h):

```cpp
void setup(void);
void loop(void);

```

---

<div class="post-metadata">

**Author:** ![Terrypin](https://avatars.discourse-cdn.com/v4/letter/t/a3d4f5/32.png) [@Terrypin](https://forum.arduino.cc/u/Terrypin)\
**Post date:** [October 16, 2022, 11:15pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/9 "2022-10-16T23:15:00Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [October 16, 2022, 11:19pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/10 "2022-10-16T23:19:13Z")

</div>

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 '{ }'

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [October 16, 2022, 11:21pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/11 "2022-10-16T23:21:14Z")

</div>

> [@Terrypin](#):
>
> But so far come across no explanation why neither setup() nor loop() have a semicolon at their ends?

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.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 16, 2022, 11:21pm UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/12 "2022-10-16T23:21:46Z")

</div>

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

> **[Why do only some lines in C++ require semicolons? I get why lines in C++...](https://www.quora.com/Why-do-only-some-lines-in-C++-require-semicolons-I-get-why-lines-in-C++-require-semicolons-x-y-+-5-this-semicolon-is-used-to-end-this-line)**
>
> Answer (1 of 15): The semicolons in c++ tell the compiler where the end of a statement is. Single line comments don't need them as that code is ignored by the compiler, it exists only to be viewed by programmers. Since the compiler skips to the next...

a7

---

<div class="post-metadata">

**Author:** ![jkwilborn](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@jkwilborn](https://forum.arduino.cc/u/jkwilborn)\
**Post date:** [October 17, 2022, 12:05am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/13 "2022-10-17T00:05:52Z")

</div>

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.

```cpp
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](https://www.bell-labs.com/usr/dmr/www/chist.html)

😺

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 17, 2022, 1:36am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/14 "2022-10-17T01:36:48Z")

</div>

> [@jkwilborn](#):
>
> The semi colon is optional.

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

---

<div class="post-metadata">

**Author:** ![ibuildrobots](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@ibuildrobots](https://forum.arduino.cc/u/ibuildrobots)\
**Post date:** [October 17, 2022, 1:48am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/15 "2022-10-17T01:48:26Z")

</div>

> [@Terrypin](#):
>
> 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?

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.

```arduino
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.

---

<div class="post-metadata">

**Author:** ![JCA34F](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jca34f/32/340148_2.png) [@JCA34F](https://forum.arduino.cc/u/JCA34F)\
**Post date:** [October 17, 2022, 2:04am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/16 "2022-10-17T02:04:53Z")

</div>

> [@ibuildrobots](#):
>
> ```arduino
> void myFunction() {
> Serial.println("In myFunction()");
> }
> 
> ```

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

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

```

---

<div class="post-metadata">

**Author:** ![ibuildrobots](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@ibuildrobots](https://forum.arduino.cc/u/ibuildrobots)\
**Post date:** [October 17, 2022, 2:44am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/17 "2022-10-17T02:44:30Z")

</div>

> [@JCA34F](#):
>
> That's the function definition, to use (call) the function:

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.

---

<div class="post-metadata">

**Author:** ![Terrypin](https://avatars.discourse-cdn.com/v4/letter/t/a3d4f5/32.png) [@Terrypin](https://forum.arduino.cc/u/Terrypin)\
**Post date:** [October 17, 2022, 11:01am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/18 "2022-10-17T11:01:55Z")

</div>

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++](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.

---

<div class="post-metadata">

**Author:** ![Terrypin](https://avatars.discourse-cdn.com/v4/letter/t/a3d4f5/32.png) [@Terrypin](https://forum.arduino.cc/u/Terrypin)\
**Post date:** [October 17, 2022, 11:08am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/19 "2022-10-17T11:08:26Z")

</div>

> [@gfvalvo](#):
>
> Post the code where you see this.

It appears in every newly started sketch.

```arduino
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!

---

<div class="post-metadata">

**Author:** ![Terrypin](https://avatars.discourse-cdn.com/v4/letter/t/a3d4f5/32.png) [@Terrypin](https://forum.arduino.cc/u/Terrypin)\
**Post date:** [October 17, 2022, 11:23am UTC](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679/20 "2022-10-17T11:23:07Z")

</div>

Thanks, got it, see my post #18.

> [@anon57585045](#):
>
> Hint: ';' is short form for '{ }'

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

```arduino
void setup()
{
}

void loop()
{
}

```

but this does not:

```arduino
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

[Next page](https://forum.arduino.cc/t/why-no-semicolon-after-the-two-mandatory-functions/1042679.md?page=2)
