Pages: [1]   Go Down
Author Topic: call function  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int Sensor1 = 2 ;
int i;
void setup ()
{
  pinMode(Sensor1,INPUT);
}
void loop ()
{Sensor_1:
 // statement...............
}

void delay_function ()
{
for(i=0;i<10000;i++)
  {
  if (digitalRead(Sensor1) == 0)
     {
       goto Sensor_1 ;
     }
  }
}

can i use goto function to Transfers program flow between different functions such as goto from void delay_function()  to void loop()
Logged

Ontario
Offline Offline
God Member
*****
Karma: 25
Posts: 890
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no.  I am not a goto-hater, but goto will not branch across functions.

If you think you need that, you better reconsider your program logic.
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Using Goto is considered very poor programming practice. It makes it very difficult to read a program and to understand the structure of the program


You just call your function like this:

Delay_function();

When it is finished doing what it should, program flow will be returnd to where you called it from.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never use goto's in a C/C++ program forget they exist. And No you can't.

Mark
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You just call your function like this:

Delay_function();

When it is finished doing what it should, program flow will be returnd to where you called it from.
i need when make specific action such as press switch exit from Delay_function() and back to the void loop (), what order can i used 
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your loop() is alway running.

So if you call your function from within loop, program flow will always be returned to loop when you exit your function.

So you need to implement the logic to read the switch in your function and then exit the function when the switch is pressed.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The C method of doing what you want is using the setjmp and longjmp functions.

Code:
#include <setjmp.h>

jmp_buf unwind_stack;

void
inner_function (void)
{
    if (some_condition ()) {
        lonjmp (unwind_stack, 1);
    }
}

void
loop (void)
{
    if (setjmp (unwind_stack) == 0) {
       /* normal code, do whatever you need here.  */
       inner_function ();

    } else {
       /* code after longjmp is called.  */
       Serial.println ("whoops");
    }
}

The C++ method uses try/catch exceptions (http://www.cplusplus.com/doc/tutorial/exceptions/).  I am unsure if exceptions are fully supported in the Arduino environment.

However, as others have said, you probably should restructure your code not to need exceptions.  I tend to think you want to use state machines, where you have a bunch of variables that say what to do, and check those each time inside of loop.  Perhaps reading and really understanding blink without a delay (the time to restart the function is a simple minded state machine) would be a start.
« Last Edit: January 07, 2013, 05:14:42 pm by MichaelMeissner » Logged

Ontario
Offline Offline
God Member
*****
Karma: 25
Posts: 890
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i need when make specific action such as press switch exit from Delay_function() and back to the void loop (), what order can i used 

Perhaps you should read about what "break" and "return" do.  You probably want to use "return".

These, together with "continue" are softer forms of goto that the goto-haters tolerate.
Logged

Pages: [1]   Go Up
Jump to: