An alternative method would be to allocate the string statically inside the function, like this:
const char * getFloat(float val, bool valid){
if (valid)
{
static char stringFloat[16];
dtostrf(val, 10, 8, stringFloat);
return stringFloat;
}
else
return "No Float";
}
Now we can use that string:
const char * c = getFloat(24.5678, true);
However note that this is only good for one use at a time!
This would not work properly, for example:
const char * c1 = getFloat(24.5678, true);
const char * c2 = getFloat(42.7654, true);
Now c1 and c2 both point to the same piece of memory, so only the last one would be correct. However this restriction may not matter to you. And it saves you having to free memory afterwards, and possibly saves you from heap fragmentation.