Can you call a function from within a function?

Thanks !

No of course not, you have never seen such a thing in any of the code here have you.

Mark

Cheers Mark. That's why it ain't working then.

What happened to your Arduino when you tried, laserbee!?

It flipped on it's back and all these noughts and ones fell out and ruined the carpet!

void loop is a function. Sorry for the stupid...x

int x = 0;    // variable

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:    
}

void loop() {  
  Serial.println(F("can u calls a function from ones??");

changex();

if(x==1){
Serial.println(F("yeppers");
} else Serial.println(F("noppers");

}

void changex(){
 x=1;
}

That should tell you if you can call a function from a function

holmes4: No of course not, you have never seen such a thing in any of the code here have you.

Mark

Of course you can. That's what recursion is all about.

Care must be exercised that the stack isn't exhausted before the recursion completes.

Laserbee - what exactly is your question ? Is it (a) "Can I call funcA() from within funcA()" or is it (b) "Can I call funcB() from within funcA()" ? The answer to both questions is yes, but if you want to do (a) then it is fraught with difficulties unless you know what you are doing. If (a) is what you want to do then then I think that you need to explain why as it may not be the best way to do what you want.

UKHeliBob: Laserbee - what exactly is your question ? Is it (a) "Can I call funcA() from within funcA()" or is it (b) "Can I call funcB() from within funcA()" ? The answer to both questions is yes, but if you want to do (a) then it is fraught with difficulties unless you know what you are doing. If (a) is what you want to do then then I think that you need to explain why as it may not be the best way to do what you want.

Hello!! can i call funcA() from funcB() and again call funcB() from funcA()??

I wonder what post # 1 was?

Paul

Yes, you can. But again you must be careful your code cannot ever get stuck in a never ending loop.. A -> B -> A -> B -> A -> B......

pcbbc: Yes, you can. But again you must be careful your code cannot ever get stuck in a never ending loop.. A -> B -> A -> B -> A -> B......

ok!! can break statement help me to get out of function in such case?

avi1992:
ok!! can break statement help me to get out of function in such case?

No.
You’d have to use return.

Paul_KD7HB: I wonder what post # 1 was?

Paul

haha... sorry mate. actually I was running through a problem where I have two functions: one for connecting to wifi and another for connecting to GSM. And while connecting to gsm , if i had failed to detect the network for GSM signal, i need to go back to wifi connecting function. Similarly, if I am connecting to WIFI and failed to do so, I need to call the function to connect to gSM. I was just surfing the internet for the relevant post and here i ended. Thanks for your effort. If you can suggest anything, it would be a great help. Have a good day.

bool connectGSM()
{
  // return false at any point to abort/error
  ...
  ...
  // success
  return true;
}

bool connectWiFi()
{
  // return false at any point to abort/error
  ...
  ...
  // success
  return true;
}

void connect()
{
  while (true)
  {
    if (connectGSM()) break;
    if (connectWiFi()) break;
  }
}

pcbbc: ``` bool connectGSM() {   // return false at any point to abort/error   ...   ...   // success   return true; }

bool connectWiFi() {   // return false at any point to abort/error   ...   ...   // success   return true; }

void connect() {   while (true)   {     if (connectGSM()) break;     if (connectWiFi()) break;   } }

ok ... let me try this and reach to you. many thanks