The question of the functions bat ()

I have a program

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7,8);
String input ="";


void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
 delay(2000);
 
 mySerial.println(F("AT+CBC"));
 
  if(mySerial.available())
  {
    String input ="";
       
    while(mySerial.available())
    {
      input+=(char)mySerial.read();
      delay(5);
    }
   
    if(find_string(input,"+CBC"))
    {
      Serial.println(input.substring(20,25));
    }   
  } 
}


boolean find_string(String base, String search)
{
  int len = search.length();

  for(int m = 0; m<(base.length()-len);m++)
  {   
    if(base.substring(m,(m+len))==search)
    {
      return true;       
    }
  }
  return false;
}

which returns

3998
4010
4015
4015
4015

The following code fragment

  if(find_string(input,"+CBC"))
    {
      Serial.println(input.substring(20,25));
    }

I moved to the function

void bat()
{
 Serial.println(input.substring(20,25)); 
}

which is called as

 if(find_string(input,"+CBC"))
    {
      bat();
    }

Why does the bat () returns nothing?

Why does the bat () returns nothing?

Because it's return type is void, and there is not return statement.

How to improve the function of the bat ()?

Do you have a specification?

What do you mean by "...improve..." ?

bat() does

Serial.println(input.substring(20,25));

What do you want it to do?

What do you want it to return?

This program

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7,8);
String input ="";


void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
 delay(2000);
 
 mySerial.println(F("AT+CBC"));
 
  if(mySerial.available())
  {
    String input ="";
       
    while(mySerial.available())
    {
      input+=(char)mySerial.read();
      delay(5);
    }
   
    if(find_string(input,"+CBC"))
    {
      Serial.println(input.substring(20,25));
    }   
  }
}


boolean find_string(String base, String search)
{
  int len = search.length();

  for(int m = 0; m<(base.length()-len);m++)
  {   
    if(base.substring(m,(m+len))==search)
    {
      return true;       
    }
  }
  return false;
}

returns

3998
4010
4015
4015
4015

Why this program

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7,8);
String input ="";


void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
 delay(2000);
 
 mySerial.println(F("AT+CBC"));
 
  if(mySerial.available())
  {
    String input ="";
       
    while(mySerial.available())
    {
      input+=(char)mySerial.read();
      delay(5);
    }
   
    if(find_string(input,"+CBC"))
    {
      bat();
    }   
  }
}


boolean find_string(String base, String search)
{
  int len = search.length();

  for(int m = 0; m<(base.length()-len);m++)
  {   
    if(base.substring(m,(m+len))==search)
    {
      return true;       
    }
  }
  return false;
}


void bat()
{
 Serial.println(input.substring(20,25));
}

does not return?

3998
4010
4015
4015
4015

I want to bat () returned

3998
4010
4015
4015
4015

I think you're confused what return means. You mean to say your program outputs that list of numbers on the serial monitor and why doesn't the other program output the same thing? Is that what you are asking?

Delta_G, you're right. Why is the second program does not display the same in the serial monitor?

I modified the program up and running. Thanks for the help.

jedneraz:
I modified the program up and running. Thanks for the help.

So you figured out that you had two different string variables named "input"?

johnwasser, You can give your way?

I did so

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7,8);
String input ="";


void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
 delay(2000);
 
 bat();
 
  if(mySerial.available())
  {
    String input ="";
       
    while(mySerial.available())
    {
      input+=(char)mySerial.read();
      delay(5);
    }
   
    if(find_string(input,"+CBC"))
    {
      Serial.println(input.substring(20,25));
    }   
  }
}


boolean find_string(String base, String search)
{
  int len = search.length();

  for(int m = 0; m<(base.length()-len);m++)
  {   
    if(base.substring(m,(m+len))==search)
    {
      return true;       
    }
  }
  return false;
}


void bat()
{
 mySerial.println(F("AT+CBC"));
}