IDE compiler code error: void function 'xyz' was not "declared in this scope."

I am experiencing a compile error when calling a void function from within another void. In this case the error generated says the called void 'xyz' was "not declared in this scope." Am I correct to think void functions do not require a declaration to call them?

This same code used within a WifiNINA EXAMPLES (ConnectWithWPA) sketch compiles without an error. The same library files are also referenced.

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(5000);
  }
                                                        // you're connected now, so print out the data
    Serial.print("You're connected to the network");
        printCurrentNet();                                                       <<<<<<<<<< error
        printWifiData();                                                            <<<<<<<<<< error

}
void loop()
    {
    
    digitalWrite(LED_BUILTIN, HIGH);                  //  blinky blinky  
    delay(200);                       
    digitalWrite(LED_BUILTIN, LOW);    
    delay(300); 
                         
      if (temp_alarm == true && temp_alarm_state == false)
       {
//       if(DEBUG){Serial.println("         Alarm is True/Send message");}
        
        temp_alarm_state = true;
        
         sendToPushingBox(DEVID1);                          //Sending request to PushingBox when the is TRUE
       }
      if (temp_alarm == false && temp_alarm_state == true)             // temp alarm is OFF
       {
 //      if(DEBUG){Serial.println("           Alarm is false/No message sent");}
    
       temp_alarm_state = false;
      }
 
    //DEBUG part
    // this write the respons from PushingBox Server.
    // You should see a "200 OK"
  
    if (client.available()) {
    char c = client.read();
    if(DEBUG){Serial.print(c);}
  }
 
  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:

   void sendToPushingBox(char devid[])
   {                         //Function for sending the request to PushingBox
    client.stop(); if(DEBUG){Serial.println("   connecting...");}
    if(client.connect(serverName, 80)) { 
    if(DEBUG){Serial.println("     connected");}
    if(DEBUG){Serial.println("     sendind request");}
    client.print("GET /pushingbox?devid=");
    client.print(devid);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(serverName);
    client.println("User-Agent: Arduino");
    client.println();
  } 
    else { 
    if(DEBUG){Serial.println("          connection failed");} 
  } 
}  
   void printWifiData() {
                                 // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");

  
  Serial.println(ip);
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  printMacAddress(mac);
}

     void printCurrentNet() {
                                      // print the SSID of the network you're attached to:
        Serial.print("SSID: ");
        Serial.println(WiFi.SSID());

                                      // print the MAC address of the router you're attached to:
        byte bssid[6];
        WiFi.BSSID(bssid);
        Serial.print("BSSID: ");
        printMacAddress(bssid);

                                        // print the received signal strength:
        long rssi = WiFi.RSSI();
        Serial.print("signal strength (RSSI):");
        Serial.println(rssi);

                                        // print the encryption type:
         byte encryption = WiFi.encryptionType();
         Serial.print("Encryption Type:");
         Serial.println(encryption, HEX);
         Serial.println();
     }

         void printMacAddress(byte mac[]) {
             for (int i = 5; i >= 0; i--) {
             if (mac[i] < 16) {
             Serial.print("0");
    }
             Serial.print(mac[i], HEX);
             if (i > 0) {
             Serial.print(":");
    }
  }
             Serial.println();
}

Error Code

******************** Error Code ********************************
/home/ed/Arduino/Uno_Wifi_Rev2_Push_Test_PushingBox_v3/Uno_Wifi_Rev2_Push_Test_PushingBox_v3.ino: In function 'void setup()':
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:56:9: error: 'printCurrentNet' was not declared in this scope
         printCurrentNet();
         ^~~~~~~~~~~~~~~
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:57:9: error: 'printWifiData' was not declared in this scope
         printWifiData();
         ^~~~~~~~~~~~~
/home/ed/Arduino/Uno_Wifi_Rev2_Push_Test_PushingBox_v3/Uno_Wifi_Rev2_Push_Test_PushingBox_v3.ino: In function 'void loop()':
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:75:10: error: 'sendToPushingBox' was not declared in this scope
          sendToPushingBox(DEVID1);                          //Sending request to PushingBox when the is TRUE
          ^~~~~~~~~~~~~~~~
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:97:4: error: a function-definition is not allowed here before '{' token
    {                         //Function for sending the request to PushingBox
    ^
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:114:25: error: a function-definition is not allowed here before '{' token
    void printWifiData() {
                         ^
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:130:29: error: a function-definition is not allowed here before '{' token
      void printCurrentNet() {
                             ^
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:153:43: error: a function-definition is not allowed here before '{' token
          void printMacAddress(byte mac[]) {
                                           ^
Uno_Wifi_Rev2_Push_Test_PushingBox_v3:164:1: error: expected '}' at end of input
 }
 ^
Using library SPI at version 1.0 in folder: /home/ed/.arduino15/packages/arduino/hardware/megaavr/1.8.7/libraries/SPI 
Using library WiFiNINA at version 1.8.13 in folder: /home/ed/Arduino/libraries/WiFiNINA 
exit status 1
'printCurrentNet' was not declared in this scope

post the code... As a general rule, things in C++ need to be declared before you can use them.

When it comes to functions (a void does not mean anything) the IDE will try to help you and pre-declare them for you in case you define them in the wrong way.

for example in theory this is not correct in C++, function A tries to call functionB which is unknown

void functionA() {
  ...
  functionB();
  ...
} 

void functionB() {
 ....
}

so the right way to do this is to define functionB first.

void functionB() {
 ....
}

void functionA() {
  ...
  functionB();
  ...
} 

but the IDE will help you and actually generate prototypes for you so the code that gets compiled looks like this

// tell the compiler those functions exist (declare them)
void functionA();
void functionB();

// now define them
void functionA() {
  ...
  functionB();
  ...
} 

void functionB() {
 ....
}

so you can write them in any order.

The problem is in the code that you have not posted

My crystal ball sees a missing } or maybe an extra {, but who knows ?

code added to original post.... :+1:

as @UKHeliBob had guessed, if you indent the code you'll see that you have a {} issue (the loop() function is missing its closing brace in the right place)

thanks....rookie mistakes no doubt... lol

But very common, hence my crystal ball being correct, and for some reason it is often the closing } of loop() that goes missing

Its kind of embarrassing really because I had checked that very thing. This shows me the importance of indentation with these functions as it makes seeing a missing brace much more apparent. Live and learn. I am a 65 year old newbie to all this...lol

Do you ever Auto Format the code in the IDE ? Bad indentation sticks out like a sore thumb if you do, particularly if you configure it to put each { and } on its own line

1 Like

Was not aware of that feature but I am now....lol
thanks for the tip... :+1:

How can anyone live without Ctrl+T ?

I even do it automatically when typing in small sections of code here then wonder why a new tab has opened !

I do ! :scream:

(I use commandt )

:innocent:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.