When print works, but when return to funcion doesn't :(

Hi.. I'm making a function to extract the decimals from a float.. and it works well when I print from inside the function, but when I use return() it shows weird characters..

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    retorna_decimais(112.78);
}

char* retorna_decimais(float vfloat)
{
    char vbuffer_float[7];
    char valor_decimal[2];
    float vvalor_subtraido = (vfloat - (int)vfloat);
    dtostrf(vvalor_subtraido, 2, 2, vbuffer_float);
    sprintf(valor_decimal, "%c%c", vbuffer_float[2], vbuffer_float[3]);
    Serial.println(valor_decimal);
}

this prints the results correctly!!!

but.. when I use return() and try to use the value returned it shows weird characters..

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    Serial.println(retorna_decimais(112.78));
}

char* retorna_decimais(float vfloat)
{
    char vbuffer_float[7];
    char valor_decimal[2];
    float vvalor_subtraido = (vfloat - (int)vfloat);
    dtostrf(vvalor_subtraido, 2, 2, vbuffer_float);
    sprintf(valor_decimal, "%c%c", vbuffer_float[2], vbuffer_float[3]);
    //Serial.println(valor_decimal);
    return(valor_decimal);
}

this returns... a?? a?? a?? a?? a?? a a a

Am I passing the values to the function wrongly?

Could anyone help me?

Tks a lot a a

You are creating a char array on the stack. When the function returns that memory is freed and all the variables internal to the function end up in the bit bucket. Make valor_decimal global, or declare it static inside the function the quickest way, with the least side effects is to:

static char valor_decimal[3]

(Make it three to allow for the '\0' at the end of the string, which may be your real problem.)

It’s very easy.
I would never do like you did. Maybe something like:

void loop()
{
        char char valor_decimal[10];
        converte_decimais(112.78, valor_decimal);
	Serial.println(valor_decimal);
}

void converte_decimais(float vfloat, char* strfloat)
{
	char vbuffer_float[7];
	//char valor_decimal[2];
	float vvalor_subtraido = (vfloat - (int)vfloat);
	dtostrf(vvalor_subtraido, 2, 2, vbuffer_float);
	sprintf(strfloat, "%c%c", vbuffer_float[2], vbuffer_float[3]);
	//Serial.println(valor_decimal);
	//return(valor_decimal);   //NO!
}

cabecinhas: Hi.. I'm making a function to extract the decimals from a float.. and it works well when I print from inside the function, but when I use return() it shows weird characters..

void setup()
{
    Serial.begin(115200);
}

void loop() { retorna_decimais(112.78); }

char* retorna_decimais(float vfloat) { char vbuffer_float[7]; char valor_decimal[2]; float vvalor_subtraido = (vfloat - (int)vfloat); dtostrf(vvalor_subtraido, 2, 2, vbuffer_float); sprintf(valor_decimal, "%c%c", vbuffer_float[2], vbuffer_float[3]); Serial.println(valor_decimal); }




this prints the results correctly!!!

but.. when I use return() and try to use the value returned it shows weird characters..


void setup() { Serial.begin(115200); }

void loop() { Serial.println(retorna_decimais(112.78)); }

char* retorna_decimais(float vfloat) { char vbuffer_float[7]; char valor_decimal[2]; float vvalor_subtraido = (vfloat - (int)vfloat); dtostrf(vvalor_subtraido, 2, 2, vbuffer_float); sprintf(valor_decimal, "%c%c", vbuffer_float[2], vbuffer_float[3]); //Serial.println(valor_decimal); return(valor_decimal); }




this returns...
a??
a??
a??
a??
a??
a
a
a

Am I passing the values to the function wrongly?

Could anyone help me?

Tks a lot
a
a

The variable valor_decimal only exists inside the retorna_decimais function. As soon as it returns, valor_decimal "evaporates", so when you try to print it, there is nothing to print.

If you declare that variable OUTSIDE the functions, it becomes global and than any function can use it.

Hope this helps.

Krupski:

cabecinhas: Hi.. I'm making a function to extract the decimals from a float.. and it works well when I print from inside the function, but when I use return() it shows weird characters..

void setup()
{
    Serial.begin(115200);
}

void loop() { retorna_decimais(112.78); }

char* retorna_decimais(float vfloat) { char vbuffer_float[7]; char valor_decimal[2]; float vvalor_subtraido = (vfloat - (int)vfloat); dtostrf(vvalor_subtraido, 2, 2, vbuffer_float); sprintf(valor_decimal, "%c%c", vbuffer_float[2], vbuffer_float[3]); Serial.println(valor_decimal); }




this prints the results correctly!!!

but.. when I use return() and try to use the value returned it shows weird characters..


void setup() { Serial.begin(115200); }

void loop() { Serial.println(retorna_decimais(112.78)); }

char* retorna_decimais(float vfloat) { char vbuffer_float[7]; char valor_decimal[2]; float vvalor_subtraido = (vfloat - (int)vfloat); dtostrf(vvalor_subtraido, 2, 2, vbuffer_float); sprintf(valor_decimal, "%c%c", vbuffer_float[2], vbuffer_float[3]); //Serial.println(valor_decimal); return(valor_decimal); }




this returns...
a??
a??
a??
a??
a??
a
a
a

Am I passing the values to the function wrongly?

Could anyone help me?

Tks a lot
a
a


The variable valor_decimal only exists inside the retorna_decimais function. As soon as it returns, valor_decimal "evaporates", so when you try to print it, there is nothing to print.

If you declare that variable OUTSIDE the functions, it becomes global and than any function can use it.

Hope this helps.

Hi!!! Yes now it worked!

What I thought(obviously wrong) for a LONG TIME was..

Well.. for me the valor_decimal variable would always handle a value because when always when the function runs it will pass the value to the variable.. (WHY NOT?)

1- the function runs.. 2- the function give value to the valor_decimal 3 -it returns its value through return(valor_decimal). 4 -the value is retrieved by who called the function..

and the funcion is called again all over again.. and again..

For me the valor_decimal would always have some value...

Now I'm confused...

It does have a value, right up until the time that bit of the stack gets written to again. Like in a timer or serial interrupt, or another function

For me the valor_decimal would always have some value..

Only inside the function where it is declared unless you declare it static in which case its value is maintained between calls to the function.

Did you also note the need for the array to be declared with 3 elements rather than 2

HI! Now I kind of understood!

Yes.. I have changed to 3 because the null character!!!

Now it becomes to be clear!!! and its working fine!!! thank you ALL!

I have started using all I need by Strings and now I know its not the best for our arduino so I'm starting to understand char, array etc..