Really simple NEWB questions... What are the { } for?

Arduino ALWAYS requires setup()

Oh no it doesn't

#include <Arduino.h>

int main()
{
  init();
  pinMode(13, OUTPUT);
  while (1)
  {
    digitalWrite(13, !digitalRead(13));
    delay(500);
  }
}

Compiles and runs for me

All credit for that - I was going to explore using main() in the IDE, but never gor around to that.
It's a really good example you should put in the Tutorial section.

Especially the reference to calling init();

Thanks UKHB -

Hi,
Although;

#include <Arduino.h>

int main()
{
  init();
  pinMode(13, OUTPUT);
  while (1)
  {
    digitalWrite(13, !digitalRead(13));
    delay(500);
  }
}

May work because of the roots of Arduino C++.
BUT:

#include <startrek.h>

void setup()
{
Serial.begin(WarpFactor3);
pinMode(deckofenterprise,spock);
}
void loop()
{
Serial.println("It isn't Arduino Code as 99% of users see it ..Jim")
Serial.println("Logic clearly dictates that the needs of the many clearly outweigh the needs of the few.");
}

Tom... :slight_smile:

I think part of the problem here is that programming, particularly in C with its huge breadth and influence, is such a broad subject.

As such, knowing what to concentrate on, and what is and is not important right now, can be daunting.

Many here, with embedded processing experience, would argue that the "do..while" construct (where the body of the loop is always executed at least once, unlike the while, or for loops, where the body of the loop may never be executed) is a waste of time.

However, someone involved in language processing, like a compiler writer, would say the do..while is invaluable, because it, like the while loop, reflects how natural and machine languages are structured, and allows simpler, more elegant mappings between problem space and program.

Some programmers will decry the ternary operator as being too terse, whilst others will defend it with second amendment zeal, because it is terse.

So, trying to find a simple, twenty minute YouTube potted C/C++ course is always going to be an exercise in frustration and futility, because such a thing cannot exist.

Furthermore, how you got to where you are now will affect how you go forward.

I have never had a C course in my life, but arrived at C from a background programming in Algol-60 (C's great grandfather) and Pascal, so many of the concepts, like scope and block structure, were already second nature.
Someone arriving at C from a background in FORTAN, BASIC or COBOL will have a different perspective, and will need some . . . remedial work.

It isn't Arduino Code as 99% of users see it ..Jim

True, but is a demo of what is possible, not what is desirable in the Arduino environment.

Thank you all for sharing.

Getting back to my original questions.....

Is it fair to say to someone just starting out a TYPICAL sketch is divided into three sections?

Global, Setup and Loop?

Setup and loop need { } but global doesn't.

Global, Setup and Loop can be in any order as Loop, setup and global, or loop, global setup, but for "best practices" the order should be Global, Setup, loop?

Varibles defied in setup are "lost" at the ending } Meaning X=10 in setup will result in X= __ in loop.
But if X=10 is in global, then in Setupup X=10 and in loop X=10.

I've been reading an it appears most commands can be used in setup as well as in loop.

Not sure if one would do this but during setup can the status of a pin or an LCD be written to?
I'm thinking it could, not sure why one would wnat to do it this way, but I guess it could be done. Maybe for debugging?

Any other advice you can give about Sketches Global, Setup and Loop to someone?
(Looking for conceptula ideas to get a better understanding of Global, Setup and Loop.)

Thanks everyone.

Is it fair to say to someone just starting out a TYPICAL sketch is divided into three sections?

Global, Setup and Loop?

Not really - if you think about it, both setup and loop are globals too.

I've been reading an it appears most commands can be used in setup as well as in loop

Both setup and loop are functions. There is nothing special in the language about either of them.

Anything - literally - you can put in loop, you can put in setup. It may not behave as you expect or wish, but that is nothing to do with the language, more to do with the structure of main/setup/loop.

int someVariable; // A global variable
void setup (void)// A global function
{

}

int anotherVariable; // also a global variable, BUT!

void loop (void) // Also a global function
{

}

Can you figure out what the big BUT is?

... a TYPICAL sketch ...

So are we talking about "typical sketches" or are we talking about "C/C++ programs"?
"setup" and "loop" have no inherent meaning in C or C++; they are just function (subroutine) names used by the Arduino environment.

divided into three sections? Global, Setup and Loop?

"setup" and "loop" are not sections. They are function definitions for functions named "Setup" and "Loop" - if you want to get the deeper understanding, you need to start separating things that are part of the language from things that are defined by the environment.

A C program has essentially TWO parts:

variable_declarations

  • function_declarations*

And then there are syntax definitions for each of those.
A variable_declaration looks something like:

variable-type name optional_initialization ;

And function_declaration looks like:

function-type name ( optional_parameter_list ) compound-statement

and compound_statement is:
{ statement_list }

(which is (one place) where the braces show up.)

The compiler folks have a standard syntax for specifying this sort of thing ("BNF") that further defines all the little parts (function-type, statement-list, etc) (recursively) until you get down to individual characters or tokens. You can fit the entire definition for C on a couple of pages.
(it gains some complexity; my examples above are "simplified.") C, as a language, pre-dates a lot of the "science", so its definition is a bit ugly. A "mini-pascal" definition fits on a single page.

How long has Arduino been out, 10 years? In 10 years no one has been able offer a simple explination?

Which "question" are you looking for a "simple explanation" for, again?
The simple explanation is "an Arduino Sketch is a simplified C program. For details, learn C."
C has been around for about 50 years. You've been given "simple explanations" several times, now, I thinkm, but you keep throwing in complications "what about Setup vs Loop, and global variables" (and other people keep throwing in complications as well.) "For details, learn compiler design. (hint: it's not actually simple.)"

What are the { } for?

Define / bracket a scope.

westfw:
The simple explanation is "an Arduino Sketch is a simplified C program. For details, learn C."

I wouldn't say "simplified". I'd say it's a specifically-structured C program. That structure happens to work well for an embedded microcontroller environment running bare metal.

Global, Setup and Loop can be in any order as Loop

Well, you have to declare global variables before you can access them, so something like

void Setup() { pinMode(myled, OUTPUT); }
void Loop() { digitalWrite(myled, !digitalRead(myled)) }
int myled = 13;  // global variable definition.

will fail to compile. If you had global variables that were referenced by some library (a bad idea!), they could theoretically be declared anywhere in your sketch.

I wouldn't say "simplified"

In this particular case, I WOULD say "simplified" because of the automatic prototype generation.
In a normal C program, you would need to define or declare function before they could be used by other functions, and that's not necessary in the Arduino environment.

void Setup() {
  int mypin = get_blink_pin();
  pinMode(mypin, OUTPUT);
}

void Loop() {}

int get_blink_pin() {
  return EEPROM.read(100);
}

would work in "Arduino", but not from a plain C/C++ program.

westfw:
In this particular case, I WOULD say "simplified" because of the automatic prototype generation.

Fair enough, I hadn't considered that. I always explicitly define all function prototypes.