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

Simple NewB questions. I've tried to research, but can't find a good simple answer.
I do not know C or C++. But I do understand the fundamentals of programming.

Can someone provide a simple explination of the sections of a sketch?
I kindof get what setup and loop are for.
But what are the { } for I thought they were functions, but was told functions are in ( ).

How do you know what goes in seetup vs. loop?

In looking at some sketches I've seen a third section in sketches with no { }. It's above setup?

Couple of other questions.
Would there ever be a time when a sketch might have two setup s ?
Or two loop sections?

How do you know what to put above setup?

And would there ever be a time where a sketch would have more than two sets of { } ?
One for setup and the other for loop.

Just looking for a simple no complicated explination or link to a reference.

Thank you.

I thought they were functions

They are.

but was told functions are in ( ).

Not by anyone that knew what they were talking about.

Arguments in calls to functions go in (). The body of a statement (a function IS a statement) goes in {}.

Now I'm totally confused. Is there web site or YouTube video that explains this correctly?

Doug101:
Now I'm totally confused. ....Is there web site or YouTube video that explains

.....this correctly?.....

Voted best rebuttal ever.

Unfortunately not everything in life is on web or even You Tube.
I'll take it that you are asking about (C) language syntax - fancy words for ANY language grammar.

If you are really interested in C language foundation - try books - "K@R" AKA Kernighan and Dennis Ritchie "C language "
or "ANSI C".

I always recommend newcomers to read "K@R" as a novel, not as a "click this , click that " instructions book.

Best of coding to you .

Any reason I just can't get a simpled explination?

It was answered simply in reply #1.

Parenthesis() contain the argument list.

Braces{} contain the body of a statement. A function is a statement.

The purpose of the setup function that only runs once at the beginning is to initialize things that need to be.
such as setting up for serial communications or an lcd or setting the pins to input or output.

The loop function continually repeats, this allows the arduino to continue executing it's programming until the arduino is turned off. otherwise it would just stop and no longer do anything once it reached the end.
There is never a reason to have two of either one. Also these are arduino specific functions.

In looking at some sketches I've seen a third section in sketches with no { }. It's above setup?

This is where you can declare/define global variables these variables can be manipulated by any function as opposed to local variables that are declared/defined within a function and can only be used in that particular function. Also you can include code that is in other files in this area.

This by no means is a complete explanation but hopefully you will get the general idea.

The following is short analysis of a blink sketch.

Let’s step back a bit from Arduino...

‘C’ and it’s peers expect a main() function, but this main() has been hidden by the Arduino IDE to make separate setup() and loop() functions that may be easier for beginners to grasp.

setup() is only ever executed once - but you can do whatever you want inside it... even ‘looping’ ! Your code could avoid the actual loop() function forever!

In most sketches, coders use the ‘one time’ setup() function to initialise the environment for their program, and then ‘fall through’ to loop() to execute the body of the program.

Arduino doesn’t give you any practical way to ‘exit’, return, or get out of loop(), so your program effectively runs forever unless you specifically derail it.

Now, beyond setup() and loop(), you may create your own function()s - just like setup and loop - to accept and return values within any part of code that you ‘call’ them.

These contain a return type, function name ( input variable list)
followed by one or more lines of codeto be executed within the function call.
e.g.
void myFunction(int aNumber) { code; }

Just to make it worse, any values you toss around and manipulate throughout your program have scope, which determines when they exist, and contain valid values. Scope may be global, local to a function, or local to any { block of code }
If you access a variable out of it’s valid scope, you’ll get a compiler error.

Added: I’ve decided to copy this post across to the tutorials section.

But what are the { } for

Computers are really dumb. The braces are to help the compiler figure out where the beginnings and ends of certain programming "constructs" are. (The compiler is what turns your program from something that looks sort-of like something human-understandable into binary bits)

Consider the famous "hard for a computer to understand" sentence:

Time flies like an arrow, fruit flies like a banana.

To make this easier for a computer to understand, you might insert some punctuation (like braces) to give the computer clues to which bits are grouped together:

Time flies {like an arrow}, {fruit flies} like a banana.

(it's actually a bit difficult to explain WHY the first version is "hard to understand", in concrete terms. You'd have to understand some things about english, and various "advanced" concepts like "compound nouns." Similarly, a detailed explanation of exactly how braces actually work requires some relatively advanced concepts from computer science.)

OK, are fruit flies ‘bugs’
That explains why my code never works, and I’m going bananas!

lastchancename:
Let’s step back a bit from Arduino...

‘C’ and it’s peers expect a main() function, but this main() has been hidden by the Arduino IDE to make separate setup() and loop() functions that may be easier for beginners to grasp.

setup() is only ever executed once - but you can do whatever you want inside it... even ‘looping’ ! Your code could avoid the actual loop() function forever!

In most sketches, coders use the ‘one time’ setup() function to initialise the environment for their program, and then ‘fall through’ to loop() to execute the body of the program.

Arduino doesn’t give you any practical way to ‘exit’, return, or get out of loop(), so your program effectively runs forever unless you specifically derail it.

Now, beyond setup() and loop(), you may create your own function()s - just like setup and loop - to accept and return values within any part of code that you ‘call’ them.

These contain a return type, function name ( input variable list)
followed by one or more lines of codeto be executed within the function call.
e.g.
void myFunction(int aNumber) { code; }

Just to make it worse, any values you toss around and manipulate throughout your program have scope, which determines when they exist, and contain valid values. Scope may be global, local to a function, or local to any { block of code }
If you access a variable out of it’s valid scope, you’ll get a compiler error.

Added: I’ve decided to copy this post across to the tutorials section.

Bravo,
tutorial like this i way long overdue.
It would be beneficial to include FULL code , maybe even in "code brackets".
( I do not believe I said that)

In my opinion - the answer is in your " that may be easier for beginners to grasp.".

That has been an underlining motto (of many) of this forum - beginners are dummies and do not deserve a real
explanation.

There is a copy of "arduino main() " somewhere, and such should be included in your tutorial.

So what if it goes over beginner head or he / she is not really interested - as long as the button
makes the LED light up.

So what if it goes over beginner head or he / she is not really interested - as long as the button
makes the LED light up.

If all someone wants is to press the button and light up the LED then all well and good, they can find the code, copy it and use it without understanding it. Then they decide that each button press should change the state of the LED and they are in trouble if they don't understand how the original program worked.

When beginners don't or won't read the advice on how to post a programming question here then there is little hope that they will make quick progress.

It’s tricky, because beginners rarely read anything that doesn’t completely solve the problem that is immediately in front of them.

I wrote this after seeing ‘a hundred’ posts with exactly the same misunderstandings and lack of awareness that you have to tell a computer what to do !

They’re not sentient beings (neither are some hobbyists!), so keeping each tutorial ‘on topic’ might help them get past the long words and pick up,at least something that will help,them i. the future.

so keeping each tutorial 'on topic' might help them get past the long words and pick up,at least something that will help,them

Writing a tutorial on a subject is an interesting exercise as without a certain basic level of knowledge you can't do anything.

When I wrote the guide to using millis() for timing I stated my assumptions about previous knowledge otherwise a tutorial on any subject has to start with "An Arduino program must have a setup() and loop() function" then has to explain what a function is, what a variable is, what scope variables have, what values they can hold, etc, etc etc all before getting to the core description of the tutorial.

OK, are fruit flies 'bugs' - That explains why my code never works, and I'm going bananas!

Indeed !!! :slight_smile:

Is there web site or YouTube video that explains

There are literally thousands of books, tutorials, videos, and classes on programming in general and programming in C in particular. You need to get to the class on "principles of compiler design and parsing", which is usually a 2nd or 3rd year university class, to get the full understanding of the exact whys and hows.

I don't really know of a "good" path to go from arduino-style "practical programming" to the CS major "deep understanding." It's complicated by the fact that if you can already DO things, some of the classes and "formality" in the path are frustratingly ... basic.

OTOH, people who get really good (and prolific) at "doing things" that never go and learn the basic theory are a bit scary...

I want to thank everyone for their replies, really helping me grasp some basic concepts.

I might be a nweb to Arduino, but I do know BASIC, PowerShell, Windows CMD shell, scripting and Linux.

If we used a car analogy. A car is a complex machine. Anyone can learn how to drive it, but that doesn't mean they understand how it works. Giving someone a simple understanding of how a car works allows them to preform some basic troubleshooting skiils. Example - If the car doesn't move is it becuase the tires are flat? Battery is dead? Or there's no gas.

If one understnad for an ICE the battery is used to provide energy to start the engine. And that the engine needs gas to provide energy keep the engine running and to power the car. And if the tries are flat the car will have an increased amout of friction which will prevent it from easily moving.

With just this simple amount of knowledge one could debug why a car woun't move.

Doug101:
Now I'm totally confused. Is there web site or YouTube video that explains this correctly?

Not sure.

There are books on the subject, they could be worth a try.

The Kerninghan & Ritchie book, already mentioned, is worth a look, although you will likely need to spend money to get one.

As a newb to Arduino can I offer a suggestion to be added to the tutorial on sketches?

If one opens a new sketch or looks at the code for Blink and other sketches it appears there are only two sections. Setup and Loop. And when I asked, I was told yes there are only two.

But then I was looking at shields and learned about libraries and the #include. Now I’m told there’s a third section about setup called Global. But now I’m wondering if there are more?

I’m still not clear if there are three or if there are any more.

Would be nice to have a simple explanation for beginners saying what the sections are and what they are used for.

Am I correct there are only 3? Global, Setup and Loop? And they should be in that order?
Could there be a second global section? As in Global Setup Global Loop ?

You would be well-served taking an online tutorial in elementary 'C' programming. It would do a much better job explaining the language's basic structure than a Q & A series in the forums.

Once you have that structure down, the forum will be much more useful to you as you learn to work within it.

Can you suggest an elementary 'C' programming online tutorial? I've looked in the past and can't find one.

I thought Arduino was suppose tot be simple.... Simple enough for 12 year olds to understand. Do they really need to learn "C" to have an understanding of Arduino?