using goto with tabs

Hello everyone!
Can i use the goto command to go from the main tab to a point in a different tab?
Normally it would be something like this right? If not then please correct me^^ I am still a beginner

void loop(){
//do stuff
function:
//do other stuff
goto function;
}

So how could i use the goto command to go to "function:" in a different tab?
And as a side question, what arethe brackets or in loop() etc? can i insert a number in there? and if so
what would that do? thatthe loop is only running 5 times for example?

Why would you want to goto another function?
What's wrong with calling it?

Noobs shouldn't be using goto.

Maybe you need to understand the function of loop() first.

loop() is called repeatedly so there is no need to jump back to the beginning.

And there is barely ever a need for goto. I haven't had a need for it in 25 plus years.

AWOL:
Why would you want to goto another function?
What's wrong with calling it?

Noobs shouldn't be using goto.

Uhm well lets say i want to seperate a long code which does stuff after a button is pressed and then jumps via goto to a different part of the code
now i seperate the code because the code after the button is pressed is too long. But that part still needs to go to that part of the main code
How else could i call a specific part of a different tab?

sterretje:
Maybe you need to understand the function of loop() first.

loop() is called repeatedly so there is no need to jump back to the beginning.

And there is barely ever a need for goto. I haven't had a need for it in 25 plus years.

Yes i know that^^ sorry i am not good with words so i might have messed up the explaining part
i know that loop() is called repeatedly. I just wanted to know why loop() {}? why not just loop {}? what is the job of those "()" What could i use instead of goto? cause i kinda need that command to realize what i have in mind

I am going to say with 99.9999999999% certainty that you don't want or need a goto.

Haha that might be true, but why wouldnt i want it? for what reasons? does it make programs run slow? or just because of readability?

Its use can make code turn into spaghetti code, where the paths and routes through the code become obscure and hard to follow.
It's much simpler if you forget it exists. (but don't worry, the compiler is clever and remembers for you)

why not just loop {}?

loop() is a function and like all functions, parameters could be passed to it if appropriate. The parameters go inside the brackets and they are required even if a function does not take parameters.

Hence

void loop()
{
  //code goes here
}

Using GOTO very quickly leads to an unmanageable mess of spaghetti code.

IMHO the very fact that you are contemplating a GOTO jump from a progam file in one tab to a program file in another tab is conclusive evidence of that.

GOTO can be used successfully by a very experienced programmer - but it would be a very exceptional situation where the more usual control systems would not work.

Have a look at how functions are used to keep the code in manageable chunks in Planning and Implementing a Program

...R

The_N00b:
. . .
I just wanted to know why loop() {}? why not just loop {}? what is the job of those "()" What could i use instead of goto?

  1. The () at the end of a function call support passing parameters from the calling program etc. into the called function.

  2. Use if,then,else,else if for selection.
    Use for - a repetition construct.
    Use functions for executing separate blocks of code.

...and don't forget "while...", "do...while", "continue" (I don't think I've ever used "continue") and "break"

As you've probably worked out, "GOTO" is the wrong word for what you are trying to do. Using that word here is like a swear word.

I think you should probably CALL another function. Call is different to goto because it will carry out the operations in that other function and the RETURN to the next line in the main program.

Calling a function requires the use of the brackets (). That tells the compiler that you want to execute that function. There are other things you can do with functions where you don't use the brackets. The brackets are also used when declaring and defining the function.

The_N00b:
Yes i know that^^ sorry i am not good with words so i might have messed up the explaining part
i know that loop() is called repeatedly. I just wanted to know why loop() {}? why not just loop {}? what is the job of those "()" What could i use instead of goto? cause i kinda need that command to realize what i have in mind

The () is an empty argument list for a function definition. Without them, the C++ compiler will think that you are trying to declare a variable and nit will complain about the braces.

The_N00b:
What could i use instead of goto? cause i kinda need that command to realize what i have in mind

Functions are called (as mentioned above). Have a look at all the standard functions that you use like digitalWrite, Serial.print or Serial.println. You don't use goto digitalWrite(somePin, HIGH); but you use digitalWrite(somePin, HIGH).

On your question why you need the round brackets; that is just in the language.

And to your original question how to call functions that are defined in a different tab (and hence a different file), you simply call them.

If you create a new file / tab and give it the extension ino, the IDE will take care of anything that is needed.

tab 1 (yourSketch.ino)

void setup()
{
  Serial.begin(115200);
  calcAndPrint(5, 7);
}

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

}

In setup you call a function 'calcAndPrint' that takes two parameters. That function is defined in another file as shown below.

tab 2 (yourFunctions.ino)

void calcAndPrint(int i, int j)
{
  Serial.println(i * j);
}

If you compile this, there is no issue. The IDE just combines all ino files in one massive ino file. It also does some other clever things like analyzing and automatically adding some of the includes before the resulting file is eventually 'passed' to the compiler.

Now e.g. libraries usually have cpp and h files. You can also do this. Rename yourFunctions.ino to yourFunctions.cpp (can be done in the IDE).

When you compile, the compiler will complain that it does not know about the function 'calcAndPrint' when it compiles; part of the error message

'calcAndPrint' was not declared in this scope

You have to tell it that it exists and you can do that using a function prototype in the file where you want to use that function (in this case yourSkect.ino).

// function prototype
void calcAndPrint(int i, int j);

void setup()
{
  Serial.begin(115200);
  calcAndPrint(5, 7);
}

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

When you compile this, there is an improvement (the above error is gone) but you have another one. In yourFunctions.cpp the compiler does not know about Serial.
Part of the error message

'Serial' was not declared in this scope

To solve that, you need to include Arduino.h in that file (the same as when you want to use e.g. the LiquidCrystal library in the main sketch).

So 'yourFunctions.cpp' now becomes

#include <arduino.h>

void calcAndPrint(int i, int j)
{
  Serial.println(i * j);
}

Now imagine what needs to happen in your main sketch if you have e.g. 20 functions in yourFunctions.cpp. You need to add 20 prototypes in yourSketch.ino. OK, doable. But now you also have another cpp file where the compiler also needs to know about (some of) those functions in yourFunctions.cpp and you need to add prototypes there as well.

That is why include files (.h) exist. Add a new tab called yourFunctions.h.

tab 3 (yourFunctions.h)

void calcAndPrint(int i, int j);
// other prototypes here
...
...
...

And just like you include a library, you can now include yourFunctions.h

#include "yourFunctions.h"
void setup()
{
  Serial.begin(115200);
  calcAndPrint(5, 7);
}

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

}

Note that there is one difference compared to the including of libraries; this uses double quotes instead of <>. These double quotes tell the compiler to first look for the specified include file in the sketch directory. If it can't find it there, it will look in the standard directories.

Although there is a little more to it, this should be sufficient for now.

Now the question that you can raise is "why go through the hassle of .cpp and .h files if I can use multiple .ino files".

And the answer is that .ino files get special processing by the IDE to basically convert them to valid C/C++ files; the compiler only 'understands' (see the earlier errors that you got while following this post) files that contain correct C/C++.

The IDE is designed to make life easy for beginners; as a result it hides a lot of mistakes that people make. As an example

void setup()
{
  doSomething();
}

void loop()
{
}

void doSomething()
{
}

The compiler will bitterly complain because it does not know about the function doSomething() by the time it encounters the call to it in setup(); the IDE modifies the above so the compilere will be happy.

You should either add a prototype before the setup() function or move the complete doSomething() function to before setup() if you were using the compiler directly.

Hope this helps.

The actual reason you cannot do it is that goto only jumps inside a function and you can't have functions cross tabs.

You can use the setjmp()/lngjmp() facility, but I do not recommend it on arduino because of the limited memory available to save the state.

AWOL:
...and don't forget "while...", "do...while", "continue" (I don't think I've ever used "continue") and "break"

I have, though I don't think it's ever been in microcontroller code. It's a simple desktop program that loops through a list of files, loads them, does some modifications or processing, and rewrites them back out if necessary. If the file failed to load, I logged an error and continued to the next file, skipping over the long processing section.

I could have done that by putting the processing in the else clause of the error check, but that adds an unnecessary indent and pair of brackets.