12

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()?
The Guy with The Hat's user avatar
The Guy with The Hat
5,3027 gold badges31 silver badges52 bronze badges
askedJun 24, 2014 at 15:48
user1584421's user avatar

3 Answers3

11
  1. Just change

    void sendSMS()

    to

    void sendSMS(const String& thisIsAString)

    You can then access the parameter inside the function withthisIsAString.

  2. No, you do not need a prototype.

answeredJun 24, 2014 at 16:46
The Guy with The Hat's user avatar
1
  • 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)CommentedJun 24, 2014 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).

answeredSep 22, 2018 at 18:45
dBm's user avatar
0

You can change

void sendSMS()

to

void sendSMS(String myinputstring)

you can usemyinputstring in your code.

answeredSep 21, 2018 at 12:47
pythosanck's user avatar
2
  • That's just a repeat of the other answer.CommentedSep 21, 2018 at 13:16
  • Not a repetition of the other answer. This one isby copy, notby reference. Prefer the other answer to avoid copying of the object.CommentedAug 29, 2020 at 11:28

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.