The same code in a function doesn't work

Hi,

I have this code that I run into the void loop and it works, then, I copy and paste the same code to make a function, and call this function in the void loop with a char array as an argument (this is the only difference, calling the array instead of just read it directly) but for some reason, it works some time (maybe 50 iterations) and then throw garbish.

Code in loop:

void loop() {
  float data;    

  union u_tag {
    byte b[4];
    float fval;
  } u;

  u.b[3] = respuesta[3];      
  u.b[2] = respuesta[4];      
  u.b[1] = respuesta[5];
  u.b[0] = respuesta[6];

  data = u.fval;
  char flotante[6];

  dtostrf(data, 4, 1, flotante);    
  String flotanteStr = flotante;
  flotanteStr.replace(" ", "");
  valores_trama = "";
  valores_trama += flotanteStr;
  valores_trama += ",";
  Serial.println(valores_trama);
  Serial.flush();
  delay(10);
}

Code in function:

void loop() {
  floatAstring(reply);
}

String floatAstring(char respuesta[]) {
///////same code as above/////////
}

Char array used:

char respuesta [9] = {0x01, 0x03, 0x04, 0x43, 0x5D, 0xA3, 0xD7, 0x46, 0xCB};     //OR renamed "reply" if used with function.

What am I doing wrong?

Please post your full code using a function. It is not obvious what

///////same code as above/////////

actually means

You almost certainly have a memory problem which almost certainly is caused by the String class.

If I was in your position I would make all String variables global, remove the String return value from the function and use the reserve method to allocate buffers for each String and see if the program runs longer than it does now.