11

Inside my main loop there is this string:

String string1;

I have a function that will take string1 as parameter, and use it to send this string as SMS.

sendSMS(string1);

This is the sendSMS() function (without parameters):

void sendSMS()
{ sms.beginSMS(remoteNumber);
  sms.print(finalstr);
  sms.endSMS();
  lcd.setCursor(0, 0);
  lcd.print("Message sent!");
  delay(10000); 
}

My questions are:

  1. How do I put the string input parameter in sendSMS?
  2. Do I also need to use a function prototype for sendSMS()? (so that it appears three times, 1 in the prototype, 1 in the declaration and one in the call). Or I don't need to use function prototype before the main loop()?
10
  1. Just change

    void sendSMS()
    

    to

    void sendSMS(const String& thisIsAString)
    

    You can then access the parameter inside the function with thisIsAString.

  2. No, you do not need a prototype.

  • 8
    I would rather advise to pass the String by reference, to avoid additional code to be executed for nothing (copy-constructor, destructor): void sendSMS(String& thisIsAString) or even better, a const reference, if the string argument is not to be modified by the function: void sendSMS(const String& thisIsAString) – jfpoilpret Jun 24 '14 at 17:05
2

I'd say to never use String again. When code gets bigger and memory usage will be critical you'll hit a dead-end. I know it's more convenient, but give char arrays a shot. Something like:

bool sendSMS(int remoteNumber, char *finalstr){
  bool isFinished = 0;
  sms.beginSMS(remoteNumber);
  for (int i=0;i<sizeof(finalstr);i++){
      sms.print(finalstr);
  }
  sms.endSMS();
  lcd.setCursor(0, 0);
  lcd.print("Message sent!");
  delay(10000);
  isFinished = 1;
  return isFinished;
}

I changed the function to bool. It means that you can use it inside an if statement, where you would want it completed before continuing with your code.

You will need to allocate memory yourself for the array; declare it like this:

char stringArray[33] = {'\0'};

Here, I allocate 32 bytes for data and one additional byte for the character that means "end of string" (it's that \0).

0

You can change

void sendSMS()

to

void sendSMS(String myinputstring)

you can use myinputstring in your code.

  • That's just a repeat of the other answer. – sempaiscuba Sep 21 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.