Return Array out of function
Don_Camillo
Posts: 3
Hello
I have a problem returning an array out of a function. I have searched this forum for a solution but did not find anything. So I hope anyone of you can help with my problem.
I have the following code:
DEFINE_FUNCTION INTEGER[nMaxSize] ArrayAddInt(INTEGER nArray[], INTEGER nItem)
{
LOCAL_VAR INTEGER nAddTempArray[LENGTH_ARRAY(nArray)+1]
LOCAL_VAR INTEGER nTempArray[LENGTH_ARRAY(nArray)]
LOCAL_VAR INTEGER nLen
LOCAL_VAR INTEGER nCount
nTempArray = nArray
nLen = LENGTH_ARRAY(nTempArray)
nMaxSize = nLen
SEND_STRING 0, "'nTempArray=', itoa(MAX_LENGTH_ARRAY(nAddTempArray))"
SEND_STRING 0, "'nLen=', itoa(nLen)"
FOR(nCount=1; nCount=nLen; nCount++)
{
nAddTempArray[nCount] = nTempArray[nCount];
}
nAddTempArray[nCount] = nItem;
RETURN(nAddTempArray)
}
nMaxSize is a global variable wich is dynamically changed to the matching size of the returning array within the function. Like this the compiler does not throw an error (because the size of the array in the function header must have the same size like the returning array). If I call this function I don't have any runtime errors, but the function does not return anything:
Integer nPort2 = 3
nTempArray2 = ArrayAddInt(nTempArray, nPort2)
The variable nTempArray is after passing this code still the same as before. Does anyone of you have an idea?
Cheers
I have a problem returning an array out of a function. I have searched this forum for a solution but did not find anything. So I hope anyone of you can help with my problem.
I have the following code:
DEFINE_FUNCTION INTEGER[nMaxSize] ArrayAddInt(INTEGER nArray[], INTEGER nItem)
{
LOCAL_VAR INTEGER nAddTempArray[LENGTH_ARRAY(nArray)+1]
LOCAL_VAR INTEGER nTempArray[LENGTH_ARRAY(nArray)]
LOCAL_VAR INTEGER nLen
LOCAL_VAR INTEGER nCount
nTempArray = nArray
nLen = LENGTH_ARRAY(nTempArray)
nMaxSize = nLen
SEND_STRING 0, "'nTempArray=', itoa(MAX_LENGTH_ARRAY(nAddTempArray))"
SEND_STRING 0, "'nLen=', itoa(nLen)"
FOR(nCount=1; nCount=nLen; nCount++)
{
nAddTempArray[nCount] = nTempArray[nCount];
}
nAddTempArray[nCount] = nItem;
RETURN(nAddTempArray)
}
nMaxSize is a global variable wich is dynamically changed to the matching size of the returning array within the function. Like this the compiler does not throw an error (because the size of the array in the function header must have the same size like the returning array). If I call this function I don't have any runtime errors, but the function does not return anything:
Integer nPort2 = 3
nTempArray2 = ArrayAddInt(nTempArray, nPort2)
The variable nTempArray is after passing this code still the same as before. Does anyone of you have an idea?
Cheers
0
Comments
I'm also pretty sure that when you initialized any VAR array the dimension has to be a constant because the variable memory allocation is set when compiled and when compiled the VAR you're using as the dimension may not have a value. Since this code is using the lenght of the array which must alraedy be intialized it might work but I'm don't recall and I'm sure I've would have tried at some point.
You should initialize your arrays to the MAX length you'll need ad then use the set_length_array keywords to increase/decrease their working size. Use max_length_array to ensure when you try to increase your array's working size it is actual capable of expanding to that size.
You can also affect an array by reference. Pass your temp array to the function, change it in the function and it will also change in the code which called the function even if its scope is local or stack since the function call is considered with in the scope of the calling code.
I just wonder that the compiler does not throw any error with the array as return value in the function header.
Thanks for your advice!
So instead of
you could try something like
And populate resultArray[] with your tempArray data.
Whatever array you pass for resultArray[] will get the data.
DH, you're absolutely right about returning arrays of intrinsic data types, but I don't think multi-dimensional arrays work. Actually, I don't think you're able to assign multi-dimensional arrays to another period. I end up getting a "GetString" and "CopyString" errors.
For you situation you could probably come up with a pretty neat solution that uses both. Something like:
Aha! Type Conversion Rule #3:
The thing is, when testing this, the compiler hollers if I try to set up a return type mismatch (LONG to INTEGER, etc.) so I assume this in another one of those "fixes" that never got mentioned.
Right, right, I know that. Sorry, it's like I wasn't even trying to make my point clear.
My line of reasoning was:
1. The Language Guide says you can't return Strings or Arrays but contradicts itself when it says...
2. You can return CHAR Arrays, i.e. Strings, and...
3. Clearly you can return arrays of other intrinsic types like INTEGERS but...
4. Assignment of a multi-dimensional array causes a "GetString" and "CopyString" error so...
5. I assume the compiler converts array assignments to "strings", but...
6. According to the type conversion rules, the compiler is forced to do a type conversion when the "value returned by a subroutine does not match the declared return type", however...
7. The compiler still hollers if I deliberately set up a return type mismatch, therefore...
(Drum roll please)
I assume the compiler allows returning one-dimensional Arrays so that Strings can be returned, and does so by treating all one-dimensional arrays as "strings". This could lead to errors like assigning a LONG array "string" to a regular CHAR string, overflowing the CHAR Array, corrupting memory, ruining lives. So you could just force type conversion and truncate the offending bits. Unfortunately this makes for frustrated programmers during runtime when their button number arrays started going above 255, so it's alot kinder to go through the effort of comparing the types during compile time and make the programmer deal with TYPE_CAST if they dare.
Thank you for indulging me. Carry on.
-Nick