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
5,2927 gold badges30 silver badges51 bronze badges
asked Jun 24, 2014 at 15:48

3 Answers 3

11
  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.

answered Jun 24, 2014 at 16:46
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) Commented Jun 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円).

answered Sep 22, 2018 at 18:45
0

You can change

void sendSMS()

to

void sendSMS(String myinputstring)

you can use myinputstring in your code.

answered Sep 21, 2018 at 12:47
2
  • That's just a repeat of the other answer. Commented Sep 21, 2018 at 13:16
  • Not a repetition of the other answer. This one is by copy, not by reference. Prefer the other answer to avoid copying of the object. Commented Aug 29, 2020 at 11:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy 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.