Help, I'm not a C ++ programmer

Greetings y'all from South Carolina. Jumping right in with my first post.

This is not my code, I got it from an instructables page for a project. I'm getting the following error;

error: return-statement with no value, in function returning 'int' [-fpermissive]

It seems related to the following block of code;

// Connect to the local Wi-Fi network
int connectWifi()
// connectWifi()
{
WiFi.mode(WIFI_STA);      
WiFi.persistent(false);        // Turn off confusing helful features
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(false);
WiFi.config(ip, gateway, subnet, dns);
WiFi.begin(WIFI_NAME, PASSWORD); 
  delay(1000);                    // Optimise for minimum value to connect
}

I think int connectWifi() is an external function that is being called and an integer value is expected to be returned.

I reached out to the code author weeks ago but haven't heard anything from him.

How do I fix this?

This is not a code.... This is part of code.

If you want a help, please, post a complete code..

Read : How to get the best out of this forum

insert a return at the end

// Connect to the local Wi-Fi network
int connectWifi()
// connectWifi()
{
WiFi.mode(WIFI_STA);      
WiFi.persistent(false);        // Turn off confusing helful features
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(false);
WiFi.config(ip, gateway, subnet, dns);
WiFi.begin(WIFI_NAME, PASSWORD); 
  delay(1000); 	// Optimise for minimum value to connect
	return 0;
}

It looks like the function is not complete... It probably have to return a error code as a result of connection to WiFi - some value in success and other if fail. Also an unconditional delay() at the end looks weird.

I worked for years as an RPG programmer on IBM systems and in those days we referred to anything from a single line of code to the entire program as code. I did not realize things had changed . I stand corrected.

Here is the entire program, sensitive information redacted;

#include <AVision_ESP8266.h>

// Water Softener Salt Level Monitor

#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
#include <ESP8266WiFi.h>

// Network information.
#define WIFI_NAME "XXXXXXX"
#define PASSWORD "XXXXXXXX"

IPAddress ip(XXX,XXX,XXX,XXX);          // Choose your ip address
IPAddress gateway(XXX,XXX,XXX,XXX);       // Your gateway
IPAddress subnet(255,255,255,0);
IPAddress dns(XXX,XXX,XXX,XXX);           // Same as your gateway

// Hardware information.
#define TIMEOUT  5000                 // Timeout for server response.

// ThingSpeak information.

#define THING_SPEAK_ADDRESS "api.thingspeak.com"
String writeAPIKey="XXXXXXXXXXXXXXXXX";             // Change this to the write API key for your channel.
                         
WiFiClient client;

// Put your setup code here, to run once:
void setup()
{  
    Serial.begin( 74880 ); // You may need to adjust the speed depending on your hardware.
    lox.begin();

    connectWifi();
}
void loop() {
      
          HTTPPost();
    
    delay( 1000 );
    Serial.println( "Goodnight for an hour ");
   ESP.deepSleep( 4200000000ULL );        // unsigned 32 bit integer in microseconds

delay(100);
    
    // If you disable sleep mode, add delay so you don't post to ThingSpeak too often.
    //   delay( 20000 );
}


// Connect to the local Wi-Fi network
int connectWifi()
// connectWifi()
{
WiFi.mode(WIFI_STA);      
WiFi.persistent(false);        // Turn off confusing helful features
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(false);
WiFi.config(ip, gateway, subnet, dns);
WiFi.begin(WIFI_NAME, PASSWORD); 
  delay(1000);                    // Optimise for minimum value to connect
    return 0;
}
    
// This function builds the data string for posting to ThingSpeak
  
int HTTPPost(){
VL53L0X_RangingMeasurementData_t measure; 
   lox.rangingTest(&measure, false);
  
    if (client.connect( THING_SPEAK_ADDRESS , 80 )){

       // Build the postData string.  
       String postData= "api_key=" + writeAPIKey ;

   postData += "&field1=";
    postData += String(measure.RangeMilliMeter);
    postData += "&field2=";
    postData += String(WiFi.RSSI());

        // POST data via HTTP.
        Serial.println( "Connecting to ThingSpeak for update..." );
        Serial.println();
        
        client.println( "POST /update HTTP/1.1" );
        client.println( "Host: api.thingspeak.com" );
        client.println( "Connection: close" );
        client.println( "Content-Type: application/x-www-form-urlencoded" );
        client.println( "Content-Length: " + String( postData.length() ) );
        client.println();
        client.println( postData );
        
        Serial.println( postData );
        
        String answer=getResponse();
        Serial.println( answer );

        Serial.println(WiFi.RSSI());
Serial.println(measure.RangeMilliMeter);
         
    }
    else
    {
      Serial.println ( "Connection Failed" );
    }    
  Return 0;
}
// Collect the response and build it into a string.
String getResponse(){
  String response;

  long startTime = millis();

  delay( 200 );
  while ( client.available() < 1 && (( millis() - startTime ) < TIMEOUT ) ){
        delay( 5 );
  }
  if( client.available() > 0 ){ // Get response from server.
     char charIn;
     do {
         charIn = client.read(); // Read a char from the buffer.
         response += charIn;     // Append the char to the string response.
        } while ( client.available() > 0 );
    }
  client.stop();
          return response;
}

Thank you.

See post #3

@jluvs2ride

Yes, a lot has changed,
even IBM has changed........... :face_with_monocle:

I worked at IBM for 20 years........

Since the function is used in the main program with possible return values ignored, simply change this:

int connectWifi()

to this:

void connectWifi()
1 Like

Then probably know more about software than most people on this forum

2 Likes

Perhaps I still retain some knowledge but the modern languages are unknown to me.

I believe I tried that.

This got me past that particular error.

I believe I tried that.

Try it again, and report what happens.

OK, so are there more?

Yeah, seems to be. This code was posted with the project. I'm surprised by the errors. Maybe different compiler versions.

This language is case specific.
return 0 is not the same as Return 0

You could also set the compiler warnings to be neglected. In this case that would work....

So by saying "Return 0;" am I telling it to perform an action if 0 is returned or just meeting the requirement of the compiler? I suppose 0 could be replaced with a variable and some action taken according to the value?

No, the compiler does not even recognize the statement Return 0;

It will recognize the statement return 0; and will return the integer value 0, which is ignored by the main program and therefore serves no purpose.