Calling function twice causes nothing to return?

The relevant section of code is below. When I call printDigits() a single time in writeDHT (or anywhere), I get the expected result, but if I call it twice as in the code below, neither call ends up happening, leaving no output to display, and I get no error message.

Pretty new to coding, and never posted on one of these forums, if there's any other information I'd be glad to provide it.

void writeDHT() {
  if (now() > lastPrint+10) {
    digitalClockDisplay(true);
    Serial.print("Humidity:    ");
    Serial.println(humAir);
    Serial.print("Temperature: ");
    Serial.print(temp);
    Serial.println(" Farenheit");
    Serial.println();
    lastPrint = now();

    //display.setFont(&TomThumb);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.clearDisplay();
    
    display.setCursor(0,16);
    display.print("TEMP: ");
    display.print(round(temp));
    display.print("F");
    
    display.setCursor(12,34);
    display.print("HUM: ");
    display.print(round(humAir));
    display.print("%");

    display.setCursor(0, 54);
    display.setTextSize(1);
    display.print(hour());
    display.print(printDigits(minute()));
    display.print(printDigits(second()));
    
    display.display();

    
  }
}

String printDigits(int digitsInt){
  // utility function for digital clock display: prints preceding colon and leading 0
  String num = ":";
  String digitsStr = String(digitsInt);
  if(digitsInt < 10)
    num = num + "0";
  num = num + digitsStr;
  return num;
}

When does that get updated with the last print value?

And it is a good idea to post all the code not just a snippet.

You are printing to a local String variable, and returning that variable, which, once the function returns, no longer exists.

So, the code is executing correctly, as written, but the function is returning garbage.

String is fine, it gets copied

I’m not sure there is overload that turns int into string the way you expect it

If only these functions were documented oh, wait:

String stringOne = String(analogRead(0), DEC);        // using an int and a base

From

HTH

a7

It seems there is.

I guess there is a possibility it is not copied as well

Why guess?

I tried it, it works fine. I cannot vouch for it working in a larger context, I cannot say it would work forever 'cause Strings, you know.

But would it makes sense to go as far as "they" did with Strings and leave off the capability for a function to return a String?

a7

because I don’t really care about String and I’m not using it myself, and because it has some unexpected overloads and the whole implementation is probably could have been done better so is it wrong to allow for that it might not have expected functionality?