PIs it possible to use a String as a return value?

If this is valid:

long timeNow ()
  {
        // Calculate time...
        return timeValue // timeValue as long
  }

Why can't I do this?

String timeNow ()
  {
        // Calculate time...
        return timeValue // timeValue as a String
  }

Which I could use like this:

  serial.println( timeNow() ):

Now I have declared a globally available static String called "returnString" that I use. It works, but I guess it would look better and be easier to use if this would be possible. Or have I missed something?

Thanks in advance!

How about posting code that has a chance of compiling? You have missing semicolons, a colon instead of a semicolon, and Serial as serial.

This compiles:

String timeNow ()
  {
  String timeValue = "foo";
  // Calculate time...
  return timeValue; // timeValue as a String
  } 
  
  void setup ()
  {
  Serial.begin (115200);
  Serial.println( timeNow() );
  }  // end of setup

void loop () { }

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Alternatively, install the fix described here:
http://arduino.cc/forum/index.php/topic,145765

You'd be better off taking the address of the (ugg!) String as an arg and modify that. Return an error code or void or something.

To add 1 char to a String, it makes a copy of itself and wipes the original which is smaller. Add another char and guess what? It won't fit in the cleared spot you get if you use the String fix. But since that erases the one it was copied from (to add 1 char) your next add a char can copy back in the now empty space. And where does the next add 1 char operation copy to? Just guess!

Strings are just Heaps of Fun.

Thanks for your replies. I managed to make String work, but it is not stable, with or without the fix.

The documentation does not mention strcpy and it's friends, so I assumed they did not exist. Next programming session will be about replacing String. It feels a bit sad having to replace these:

String decodeURL( String in ) { // Data from GET
  in.replace("%20", " ");
  in.replace("%C3%A5", "å");
  in.replace("%C3%A4", "ä");
  in.replace("%C3%B6", "ö");
  in.replace("%C3%85", "Å");
  in.replace("%C3%84", "Ä");
  in.replace("%C3%96", "Ö");
  return( in );
}


String SWE( String in ) { // Translate characters to the web client
  in.replace("å", "å");
  in.replace("ä", "ä");
  in.replace("ö", "&oring;");
  in.replace("Å", "Å");
  in.replace("Ä", "Ä");
  in.replace("Ö", "&Oring;");
  return( in );
}

You have to go to the roots of the C we use to get the good stuff.

AVR LibC

Modules
Here is a list of all modules:

http://www.nongnu.org/avr-libc/user-manual/modules.html

strings:
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

There's piles of good tutorials on C strings.

The string functions like strncpy() have a pattern, the n is count and they don't add a terminating zero. That cuts the number of mnemonic parts you have to recognize/remember.

Now I have learned something. There is a reason they made String! :slight_smile:

Okay, my big problem was Ethernetshield-related. I tried another and it worked fine, but at that point had I already started migrating from String.

I need help with this:

I converted my SWE-function (see above) to live without String. Now it looks like this, and relies on a replace-function found with Google.

void SWE( char * in ) { // Translate characters to the web client
  replace(in, "å", "å");
  replace(in, "ä", "ä");
  replace(in, "ö", "&oring;");
  replace(in, "Å", "Å");
  replace(in, "Ä", "Ä");
  replace(in, "Ö", "&Oring;");
  replace(in, "º", "°");
  replace(in, "°", "°");
}

Using it, I can convert char arrays. Great if they are variables.
But I can't convert this:

client.println("
");

To get around this problem I tried to put client.println() into the SWE-function, but it did not work.
Then I tried to use a pointer to it, but I can not make it compile "C.println(in);". I get the error "request for member 'println' in 'C', which is of non-class type 'EthernetClient*' ":

..
//Where I send html to the client
SWE("
", recTemp);
..


void SWE( char * in, EthernetClient * C ) { // Translate characters to the web client
  replace(in, "å", "å");
  replace(in, "ä", "ä");
  replace(in, "ö", "&oring;");
  replace(in, "Å", "Å");
  replace(in, "Ä", "Ä");
  replace(in, "Ö", "&Oring;");
  replace(in, "º", "°");
  replace(in, "°", "°");
  C.println(in);
}

Any ideas? Later, I would like to move the HTML to an SD card. That must have been done before? Perhaps with some functionality to put in variable values?