Using goto

Can I call a label above the function call of that label?
:confused:

Except when you mean it in an experimental way, just don't use goto. It just makes a big mess of the code. Just use proper functions.

Why do you feel the need to use goto ? Using it results in code that is difficult to follow. You are much better of without it.

The answer is yes, the label can be anywhere in the function.

Thanks for your help I will be considering all your replies........... :slight_smile:

hi ajay_roy, Take a look at this thread (goto - ever used it?) It's full of coding examples, philosophical ideas, admonitions, and encouragement, all surrounding goto.

ajay_roy:
Can I call a label above the function call of that label?
:confused:

Can you give an example, so we know we're on the same page?

ajay_roy:
Can I call a label above the function call of that label?

Just out of curiosity, why don't you try? That would be a lot quicker than typing up a forum question.

Oh, and by the way: Don't use goto!

ChrisTenone:
hi ajay_roy, Take a look at this thread (goto - ever used it?) It's full of coding examples, philosophical ideas, admonitions, and encouragement, all surrounding goto.

Server not found Chris, check your link. Don't use the WYSIWYG mode in the forum - it is crap.

Garrr. Thanks for the hu Nick.That is exactly what I did, and there were some wrong-way slashes, way too many quotes, TWO http://'s and it said iurl. What the heck is iurl??

ChrisTenone:
Garrr. Thanks for the hu Nick.That is exactly what I did, and there were some wrong-way slashes, way too many quotes, TWO http://'s and it said iurl. What the heck is iurl??

The forum tries to help you. It fails.

As a professional programmer using goto is the worst kind of sin. But then most languages have a go sub and return statement. Since Adriano doesn't sully that statement it is safe to say this is no stack on the chip.
I would use a goto statement here to simulate a subroutine. That means using a second goto, to return from whence I came I.e.

Goto sub1
:home1

Goto endofloop
:sub1
Do my my processing here
Goto home1

:endofloop

All sub routines would have to be at the end of the main loop proceeded by a goto the end of the loop to prevent the program from dropping though the sub routines. Admittedly it is messy but could eliminate a lot of duplicate code if needed.

Mrkite:
As a professional programmer using goto is the worst kind of sin. But then most languages have a go sub and return statement. Since Adriano doesn't sully that statement it is safe to say this is no stack on the chip.
I would use a goto statement here to simulate a subroutine. That means using a second goto, to return from whence I came I.e.

Goto sub1
:home1

Goto endofloop
:sub1
Do my my processing here
Goto home1

:endofloop

All sub routines would have to be at the end of the main loop proceeded by a goto the end of the loop to prevent the program from dropping though the sub routines. Admittedly it is messy but could eliminate a lot of duplicate code if needed.

Maybe I'm missing something, but that seems to be the long way around. Why not just call sub1 as a function? Then, after completion, it would return to the place where it was called, ... through the (invisible and automatic) use of the stack.

Mrkite:
But then most languages have a go sub and return statement. Since Adriano doesn't sully that statement it is safe to say this is no stack on the chip.
I would use a goto statement here to simulate a subroutine.

What on Earth are you talking about?

Arduino IDE implements C++. You have function calls, classes, templates, the whole shebang.

it is safe to say this is no stack on the chip

Yes, there is a stack. And a heap. It has dynamic memory allocation if you want it. It has interrupts. It has the proverbial kitchen sink.

But then most languages have a go sub and return statement. Since Adriano doesn't sully that statement it is safe to say this is no stack on the chip.

In C (and C++ and Arduino) you define a function (or "subroutine", which is the same thing, except some languages call code a "subroutine" if it doesn't return a value and a "function" if it does.) just by typing it in:

long pow10(int exp) {
 // Compute 10 to the power X, as integers
  long result = 1;
  while (exp > 0) {
    result = result * 10;
    exp = exp - 1;
  }
  return result;
}

And then you do the equivalent of "gosub" by using it like a function:

num = num + digit * pow10(n);

or if it didn't return a value, you'd just do:

digitalWrite(pin, val);

You should notice that all of the arduino-defined special functions (like digitalWrite()) are subroutines, as are loop() and setup(), and you can define as many as you want (within the limits of memory, of course.)

PS: I think "we" should recommend functions more often in the forums...

westfw:
PS: I think "we" should recommend functions more often in the forums...

Yes yes yes! People always try to dump everything right into loop while making all kinds of smaller functions is easier to do and easier to bugfix.