Still a bit stuck with { }, return and exit.

Yeah, I am going to cop some flack for this, but hey......

I'm still learning.

Brief explanation of my "problem":

C is "different" to BASIC in that you write the code in blocks.
Although this can be likened to "sub-routines" in basic there are differences in how the code is defined.

In BASIC you RETURN from a sub-routine and any values are GLOBAL.

C is very different that variable names are local to that routine and passing variables is done a bit differently.

However, the "blocks" of code.

You start with something like:
(Indulge me)

Routine_name()
{
    //  code here
}

So when I am writing a program it looks like this (sort of):

loop()
{
     // things happen here
     routine_1();

     //  a bit more stuff
     routine_2();

     // and some more stuff here.
    int(j) = routine_3();
    if(j == 1)
    {
          // what ever
     }
     else
     //  and so on.
}

Now "routine_1" is a bit of code which is used in many places in the "bigger picture" of the code.

It is made into a routine to save space and reduce duplication of lines.
I get that.
But "how" do I write "routine_1" (and others) code?
Hang on!

So, the routine_ would be like I posted above.
It starts with a name and the {
The first line says if it returns a variable by either having an INT, VOID or other prefix.
How it parses variables is done after the name, like this: (for instance)
routine_1(int var_1)
{

Tells it that there is nothing returned and that VAR_1 is given to it.

So: When I an done writing routine_1, how do I "end" it?

Is it simply to "close" off the { }'s?
Or do I "exit"?

Yeah, dumb question, but some one has to ask them.
May as well be me.

a function (routine) as you pointed out can be declared or returns void

void loop()
{
  //code here
}

void someRoutine()
{
  // this function returns no value but runs code here
}

int otherRoutine(int value)
{
  //returns an int
  //expects an int to run (int value)
  return someVariable;
}

variables have scope of Global, Function and block {}

lots to absorb...

The function ends at the second bracket: no "exit" used
void function1() // type void will not return a value with the return() instruction
{
code;
} // function ends here. The C compiler automatically returns to the next code to execute
example of a function example of type int :
int function() // type int can return the value of an integer, declared within the function
{
int data = 5;
code;
return( data );
} // the return means that it returns the value of data , which is 5. The funtion still ends because of the second bracket but it passed the int value of 5 to a variable with code like:
int variable_X;
variable_X = function(); // now variable_X equals the value of the int data from inside the function:

If a variable is declared outside of a function, then it can be accessed by other functions, it is global and the value does not need to be returned.
if a variable is declared within a function it is deleated at the end of the function. In the above example it's value was passed to another variable before it was deleted by the "return(data)" instruction:

Try to find a good C manual to guide through all the data types and functions. There is a lot to learn, but C C++ is a widely used language and worth learning

Fentrac:
The function ends at the second bracket: no "exit" used

except a special function called loop() which loops (nice, right)

} // function ends here. The C compiler automatically returns to the next code to execute

actually returns to the line following where it was called

Hi guys.

Thanks very much for the help.

I have "shot my self in the foot" with some code I am writing now, so I shall fix it up.

I am sort of coping with variables pretty good, and am trying to do things in a constant way so as to "learn" that pattern.

It is (probably/most likely) different to the INDUSTRY STANDARD way, but hey: If it works.

My style of programming has really changed since I first started writing code, and I hope for the better.

I am waist deep in a couple of projects just now and needed to really get this resolved to stop future problems.

So: Onward and upward, I hope.

This might clarify, if you haven't already read it:

lost_and_confused:
Is it simply to "close" off the { }'s?

A "subroutine" (function) optionally takes and optionally returns data.

For example, a function to add two integer numbers:

int add_two (int a, int b)
{
    int answer = (a + b);
    return answer;
}

The "int" before the name "add_two" means the function will return an integer number. The ints in the parentheses define the variables "a" and "b" as integer numbers.

Inside the function, a variable named "answer" is declared and at the same time set to the value of "a + b".

Lastly, it's returned to the caller.

Now, a function that returns a number, but takes in nothing (useless, but it will help you understand the differences):

int useless (void)
{
    int a = 3;
    int b = 5;
    int c = (a + b);
    return c;
}

All this does is return the number "8" in the form of an "int" which is a signed 16 bit number (meaning it can range from -32768 to +32767).

Notice that since it takes in nothing, the parameter is called "void". But, it returns an int, so "int" precedes the function name.

Now, put it all together (with a little pseudo code for simplification):

int main (int argc, char *argv[])
{
    print "Enter the first number: " // not real C code
    int a = get_input (); // hypothetical function that gets user input
    print "Enter the second number: "  // not real C code
    int b = get_input (); // hypothetical function that gets user input
    int answer = add_two (a, b); // call add_two, place return value in "answer"
    print "The sum of the two numbers is answer" // "answer" actually prints the variable value, not the word "answer"
}

Hope this makes sense....

BulldogLowell:
except a special function called loop() which loops (nice, right)

No, it ends at the second brace. It's just recalled again by a for loop in a main function that you never see when you use the Arduino IDE.

Jiggy-Ninja:
No, it ends at the second brace. It's just recalled again by a for loop in a main function that you never see when you use the Arduino IDE.

yes, that's what I said, it loops

You really need to beg, borrow or steal a competent textbook on C++ , and read it. It will save you time in the long run.

BulldogLowell:

Fentrac:
The function ends at the second bracket: no "exit" used

except a special function called loop() which loops (nice, right)

There is nothing at all special about the function "loop()".

AWOL:
There is nothing at all special about the function "loop()".

After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.

As far as functions go, it does distinguish itself from the rest, even if someone doesn't think it is "special".

it does distinguish itself from the rest,

In that it gets called repeatedly?

AWOL:

it does distinguish itself from the rest,

In that it gets called repeatedly?

void setup()
{
  Serial.println("We are now in the setup() function");
  Serial.println("hello World");
  someOtherFunction();
}

void loop()
{
  Serial.println("We are now in the loop() function");
  Serial.println("hello World");
}

void someOtherFunction()
{
  Serial.println("We are now in the someOtherFunction() function");
  Serial.println("hello World");
}

doesn't the loop() function behave differently than someOtherFunction?

even if just a little bit?

loop() is different because it gets treated in a unique way when you compile it as compared to colleague someOtherFunction(). To me, that makes it special. :smiley: In fact, I'm may even call setup() special from now on too. It seems to get treated differently when compared to its function friends.

but this is semantics but not an argument not 100% in vain.

the OP should now understand how some functions are unique, different, special,...

By that argument, "println" is also "special", because you've called it multiple time in both "setup()" and "loop()".

If anything, only "setup" is special, because it is not called repeatedly, but of course, being just another C function, it isn't really special at all.

@michinyon: Given the level of questions the OP is asking, I think a better starting place is a simple book on C. Obviously, I have my own preferences, but in any case, it would be easier to learn C and then move to C++.

As to the loop() discussion, if you look at the code in main.cpp off the "cores\arduino" directories, you'll find:

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

This is the code that actually "runs" the program. It's pretty easy to see how setup() and loop() work under the covers of the IDE.

It's a great shame that the people who named the loop() function didn't put more thought into the name. It's very common for newcomers to initially be confused about looping control structures and about using functions, and calling a function 'loop' just add to the confusion.

BulldogLowell:

Jiggy-Ninja:
No, it ends at the second brace. It's just recalled again by a for loop in a main function that you never see when you use the Arduino IDE.

yes, that's what I said, it loops

But that loop includes a function return and function call which will destroy all automatic variables and then recreate them. It is extremely misleading to consider that a "loop".

KeithRB:
But that loop includes a function return and function call which will destroy all automatic variables and then recreate them. It is extremely misleading to consider that a "loop".

really?

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

BulldogLowell:

KeithRB:
But that loop includes a function return and function call which will destroy all automatic variables and then recreate them. It is extremely misleading to consider that a "loop".

really?

#include <Arduino.h>

int main(void)
{
init();

#if defined(USBCON)
USBDevice.attach();
#endif

setup();

for (;:wink: {
loop();
if (serialEventRun) serialEventRun();
}
       
return 0;
}

OK. What does that say about destroying and re-creating automatic state?

For example:

int a =0;
int b=0;
int c = 0;
for (;;)
{
    a++; b++;
    c = a+b;
}

and this:

for (;;)
{
    loop();
}

void loop()
{
    a=0;
    b=0;
    c = 0;
    a++;
    b++;
     c= a+b;
}

One is a loop, the other is a function that gets called repeatedly, big difference.