Returning an array (DIM) from a function

Previous topic - Next topic

François Vanzeveren

Hello,

I am wondering how to define a local array within a function.
I tried this
Code (glbasic) Select

LOCAL DIM myArray[10];

but this does not work. If i left out the word LOCAL, then it looks ok. Is it normal? Is myArray still local?

Secondly, is it ok to return a local array from a function like this:

Code (glbasic) Select

FUNCTION BuildSequence: pLevel
// These values are defined LOCAL:
// pLevel
DIM tSequence[pLevel+2];

FOR i = 0 TO pLevel+1
tSequence[i] = RND(7);
NEXT
RETURN tSequence
ENDFUNCTION


How should the "receiving" variable look like?

Thanks for your help.

François

Kitty Hello

Code (glbasic) Select

FUNCTION foo:
LOCAL arr[]
DIM arr[120]

You can't return an array. But arrays are passed by reference, thus you can use an array as a parameter and change the contents within the function.

François Vanzeveren

Marvellous, it even better.

Thanks for your quick answer.

François

Schranz0r

example:

Code (glbasic) Select

//Create a array
LOCAL MyArray[]
DIM MyArray[12]

//fill something into it
SetSomething(MyArray[])

WHILE TRUE

// Print all
FOR i = 0 TO LEN(MyArray[])-1
PRINT MyArray[i], i*20,10
NEXT

SHOWSCREEN
WEND
END



FUNCTION SetSomething: Array[]

// from 0 to Arraysize -1
FOR i = 0 TO LEN(Array[])-1
Array[i] = i // fill in
NEXT

ENDFUNCTION

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

François Vanzeveren

Thanks for the exemple... it really helps  :)

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard