Subroutines, calling and program flow

Can someone tell me

  1. What syntax I use to call these 3 subroutines.

  2. Do they have to be declared in setup

  3. What happens at the end of a subroutine, do they return like a basic gosub

  4. If I want to go somewhere within a subroutine, how do I do it without a GoTo type command

if (digitalRead(pins[x]) == HIGH)
    {
      Call a subroutine1
    }
    else if (digitalRead(pins[x]) == LOW)
    {
      Call a subroutine2
    }
    else
    {
     Call a subroutine3
    }

  
void subroutine1();

void subroutine1();

void subroutine1();

First of all they are functions.

Second, they are defined outside of any other function. The arduino (badly) takes care of declarations, so you only have to define them. I prefer my functions to be below setup and loop, but the compiler does not care. Use structured loops like if, for and while to control flow in a function:

void setup() {}

void loop()
{
if (digitalRead(pins[x]) == HIGH)
    {
      subroutine1()
    }
    else if (digitalRead(pins[x]) == LOW)
    {
      subroutine2()
    }
    else
    {
     subroutine3()
    }


}

void subroutine1(void)
{
  // takes no arguments and returns nothing
  // do sub 1 stuff
  // returns back to where it was called, like gosub
}


void subroutine2(void)
{
  // takes no arguments and returns nothing
  // do sub 2 stuff
}

void subroutine3(void)
{
  // takes no arguments and returns nothing
  // do sub 3 stuff
}

Many thanks Keith for making it so clear.

I am certainly missing the GoTo command, but at least won an equivelent to a Gosub.

Regards

if (digitalRead(pins[x]) == HIGH)
    {
      Call a subroutine1
    }
    else if (digitalRead(pins[x]) == LOW)
    {
      Call a subroutine2
    }
    else
    {
     Call a subroutine3
    }

  
void subroutine1();

void subroutine1();

void subroutine1();

As digitalRead() will only return HIGH or LOW only one if and an else is required to deal with all possible conditions.

  1. If I want to go somewhere within a subroutine, how do I do it without a GoTo type command

Do you mean that you want to enter a function at some point within it (not possible) or jump around once in the function (ill advised) but you can get the same effect using conditions.

Many thanks. I simply put the If,else routine up to ask how such subroutines could be called. Yes there would ony be a high/low instance there so not needing the third slot.

But thanks for coming in.

You have a goto command, but its use is discouraged. In practice, there is no need for goto since C has a complete set of structured control operators. As K&R say," C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book....Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all."

I have written thousands of lines of code and have used goto only a few times. Generally because of poor planning on my part. (I felt that re-writing would introduce more bugs than adding the goto.)

However, you can only goto inside a function, so no jumping all over your code. It looks like this:

void function1(void) 
{

   label1:
   // create a label
   // do some stuff
   goto label1; // jump!
}

Just maybe, you need to learn the language. C/C++ would not have lasted so long if they would have been crippled in the the way you imagine.

Do you mean that you want to enter a function at some point within it (not possible) or jump around once in the function (ill advised) but you can get the same effect using conditions.

Apparently, it was going to be possible. Original C had an "entry" keyword reserved to allow for multiple function entrances. Unfortunately for obfuscated C contests, it was never implemented.

Depends on the function. digitalRead is a function and it requires an argument. In your code you're passing it the argument pin[x].
digitalWrite is a function that requires 2 arguments.
So how you cal it depends on what the function requires.

Yes, they return to the function that called them.

ZOR2:
Many thanks Keith for making it so clear.

I am certainly missing the GoTo command, but at least won an equivelent to a Gosub.

Regards

Learn about while, for, break, continue, and switch. Do that before any more programming.
They are higher level and between them cover most anything you might think needs a goto.

In better languages than C and C++ you get structured exceptions, which take care of
all the other cases, and help write clearer code by separating exceptionally flow from normal
control flow.

Many thanks both, that makes it a lot easier to understand.

I will try everything to avoid using goto, guess it's all down to getting the structure correct. I did a flowchart for what I am trying to achieve, it's the fun of seeing how I manage it and adhere to in my code.

It's learning all the building blocks first thats important. Thanks again

Thanks Mark, sorry missed seeing your reply in exchanging replies. I will take your advice and look those items up now. Thanks

Use structured loops like if

Is is NOT a looping construct!

PaulS:
Is (if) is NOT a looping construct!

Yes a minor semantic slip up there by Keith (we all make them every now and then). I'm sure he meant to say flow control constructs. :wink:

PaulS:
Is is NOT a looping construct!

It is if you add goto. 8^)

KeithRB:
It is if you add goto. 8^)

It is could be if you add goto. :wink:

Keith, you're reminding me of the "numerical" if statement in some of the really ancient versions of Fortran.

IF (X-Y) 10, 20, 30

Oh no, please let me forget it. :slight_smile:

Oh no, please let me forget it. :slight_smile:

I had. Thanks for dragging THAT back up.