[Solved] Invoking/calling a separate function from within a function

I have googled the subject and it is supposed to be possible - but my attempt at doing it is not working.
Could someone please advise where I went wrong.

The following code reads a file on an SD card and invokes a error function on error - it all works.

//Read file:
if (pf_open(myfile) != FR_OK) errorHalt("pf_open ");
    while (1) {
      UINT nr;
      if (pf_read(buffer, sizeof(buffer), &nr) != FR_OK) errorHalt("pf_read");
      if (nr == 0) break;
      Serial.write(buffer, nr);
    }


// PetitFS Error Function
void errorHalt(char* msg) {
  Serial.print(F("Error: "));
  Serial.println(msg);
  while(1);
}

I now want to read multiple files, so I want to create a read function, which I attempted like this :

//  Read Pfat File Function:
void Read_sdCard_File_PFat (char myfile[]) {
    if (pf_open(myfile) != FR_OK) errorHalt("pf_open ");
    while (1) {
      UINT nr;
      if (pf_read(buffer, sizeof(buffer), &nr) != FR_OK) errorHalt("pf_read");
      if (nr == 0) break;
      Serial.write(buffer, nr);
    }
 }

However, the calling of the errorhalt() function from within the new read function throws up the following compile error.

In file included from SH-RF433_Arduino_Rx.ino:99:0:
functions.h: In function 'void Read_sdCard_File_PFat(char*)':
functions.h:25: error: 'errorhalt' was not declared in this scope
if (pf_open(myfile) != FR_OK) errorhalt("opening file");
^
functions.h:28: error: 'errorhalt' was not declared in this scope
if (pf_read(buffer, sizeof(buffer), &nr) != FR_OK) errorhalt("reading file");
^
'errorhalt' was not declared in this scope

FWIW both functions are on a functions.h tab which is included before setup i.e. in the header.
Appreciate any guidance.

And we're supposed to explain why code you're not showing us doesn't work?

I suppose the problem is that your function errorHalt is declared after Read_sdCard_File_PFat. In the main sketch file it would work because the Arduino IDE will automatically forward function declarations, but it doesn't do that in other files such as your functions.h file.

http://www.learncpp.com/cpp-tutorial/17-forward-declarations/

guix:
I suppose the problem is that your function errorHalt is declared after Read_sdCard_File_PFat. In the main sketch file it would work because the Arduino IDE will automatically forward function declarations, but it doesn't do that in other files such as your functions.h file.

http://www.learncpp.com/cpp-tutorial/17-forward-declarations/

I tried placing the errorhalt() function before the read function in the .h tab, but I still get the same error.
I also tried placing a function prototype of errorhalt() inside the read function - that did not help either.
:frowning:

Edit: Actually in the previous attempt I had been messing with the code and was trying to use a different function. To test, I inadvertently manually typed errorHalt() with a lower case h, which gave the same error.
I have since fixed the code and with the read function at the bottom of the tab - low and behold it compiles.
Thanks guix - it appears you were right after all. :slight_smile:

void errorHalt(char* msg) {

...

functions.h:25: error: 'errorhalt' was not declared in this scope
     if (pf_open(myfile) != FR_OK) errorhalt("opening file");

Problem.

Do you see it?

Oops just deleted my previous post.
Ah yes, lower case h vs uppercase H - good spot!

BUT...
Now it makes sense...
I moved the original post from another section - since I received no response for 2 days.
In the process I inadvertently deleted the original error message.
To recreate the error message I manually edited the function using lowercase h.
But the original code had uppercase H and was correct.
So the problem was in fact not the lower/uppercase h/H issue.
It was the positioning as pointed out by guix.

I double checked and with the read function before the error function, it does not compile.
With the read function after the error function, it compiles.