Function does not return variable?!

Hey all,

I’m used to PHP and there I can do such a simple function;

function testFunction( varTest )
{
	// do something
	// fill variable returnVar
	
	return returnVar;
}

However, that doesn’t seem to work in C or Arduino language…

char encodeData( char *dataPack )
{
    int dataPackLen = sizeof(dataPack);
    
    int encodedLen = base64_enc_len(dataPackLen);
    char encoded[encodedLen];

    base64_encode(encoded, dataPack, dataPackLen); 
    
    Serial.println(encoded);
    return(encoded);
}

I call this above function on the Arduino with the following;

encodedDataPacket = encodeData(dataPacket);
Serial.println(encodedDataPacket);

I’ve tried so many combinations of char encodeData( char *dataPack) with char *encodeData, char dataPack and encodeData(*dataPacket) and all sort of weird combinations… I really can’t keep track of everything I’ve tried, but my head has already exploded and I’m wondering if I’m even gonna pick up the pieces from the floor!

Oh well…

Within the encodeData function, the Serial.println(encoded) does print out the base64 encoded string properly, nothing strange there…

But returning the encoded variable… that doesn’t work! All I get in return is the character ‘~’
NO FUN! :wink:

I’ve tried return encoded; as well, without the braces… no difference…

What am I doing wrong?

I had a look at the base64_encode function itself, to see how they solved the return. That function replaces the content of variable encoded that you send into the function with the actual encoded data as a return.

int base64_encode(char *output, char *input, int inputLen) {
	int i = 0, j = 0;
	int encLen = 0;
	unsigned char a3[3];
	unsigned char a4[4];

	while(inputLen--) {
		a3[i++] = *(input++);
		if(i == 3) {
			a3_to_a4(a4, a3);

			for(i = 0; i < 4; i++) {
				output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);
			}

			i = 0;
		}
	}

	if(i) {
		for(j = i; j < 3; j++) {
			a3[j] = '\0';
		}

		a3_to_a4(a4, a3);

		for(j = 0; j < i + 1; j++) {
			output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);
		}

		while((i++ < 3)) {
			output[encLen++] = '=';
		}
	}
	output[encLen] = '\0';
	return encLen;
}

That would be ok as well, so I dont have for example;

variable = function(something);
Serial.println(variable);

Instead, it would be;

function(something);
Serial.println(something);

But after setting up the call and my function identically to the base64_encode function, it still doesn’t work!

Please, what am I missing? Really need your help!

Thanks in advance!

You're returning a pointer to something that is about to go out of scope when the function returns. That's not going to work. Either pass the pointer to the array you want returned to the function or make the array static

However, that doesn't seem to work in C or Arduino language..

Sure it does. But, the type you want to return must be defined AND what you try to return has to be of that type.

chrisfredriksson:
However, that doesn’t seem to work in C or Arduino language…

char encodeData( char *dataPack )

{
   int dataPackLen = sizeof(dataPack);
   
   int encodedLen = base64_enc_len(dataPackLen);
   char encoded[encodedLen];

base64_encode(encoded, dataPack, dataPackLen);
   
   Serial.println(encoded);
   return(encoded);
}

Didn’t you get a compile error or warning when you tried to return “char encoded[encodedLen];” (a character array) as a single character?!?