News:

Function Finder  Find all the functions within source code files

Main Menu

Can Functions Return Arrays?

Started by SpellSword, August 16, 2007, 08:45:51 PM

Previous topic - Next topic

SpellSword

Is this possible?
Example
Dim Array(4,3)
Array()=Test(Array())

Function Test(Array())
REM <<<CODE to modify the Array() somehow>>>
EndFunction Array()

(I think I typed that right...)

I get an error saying: "EndFunction may only return variables or constants." when I run it from the Editor.

I realize substitutions for passing to a function are capable using GOTO and GoSub, I'm just hoping there's a trick to using arrays with functions in this manner.


EDIT:  YES, this is added in PB1.64  See here See Here

When I dream,
I carry a sword in one hand,
a gun in the other...

kevin

#1
 You couldn't pass them out back in August 16, 2007

Ie.  You can't do this today



MakeArray Me()

Me() = CreateArray(10,20,30)


Function CreateArray(a,b,c)

 Dim LocalArray(a,b,c)

EndFunction  LocalArray()     ; This is not supported..



However, you can pass in, so it doesn't really matter. It's just shift in logic.

So this does the same thing as the above.


MakeArray Me()

CreateArray(me(),10,20,30)


Function CreateArray(LocalArray(),a,b,c)

 Dim LocalArray(a,b,c)

EndFunction




  MODEDIT: This information has been obsolette for years,  modern editions of PlayBASIC do indeed support return arrays from functions


SpellSword

I can see how the data is passed to the array and the LocalArray() is given the dimensions of 10,20,30, but I'm not sure how your example returns the data stored in the array from the function.

I've been tinkering with the code and haven't produced a returned variable yet. I think it has something to do with the Me() PointerArray... (Just finished reading MakeArray PB Help tutorial.)

What I've done so far:
| I've tried accessing the Me() after the function call.
| I've tried accessing the function's LocalArray() from outside the function after the function call. (Just incase it had been made global somehow.)
| And, I've tried to use the function CreateArray() as an array in a fit of madness that, of course, produced no results. (Unless I did it wrong and that is indeed the correct path?)

Am I even close to being on the right track?  ???
When I dream,
I carry a sword in one hand,
a gun in the other...

empty

Basically, Arrays are passed to functions by reference. That means that not the content of the array is passed to the function, instead a pointer is passed. So anything you do with that array within the function will not merely affect a local copy of that array but the passed array itself.


Dim MyArray(2)

Test(MyArray())

for i = 0 to 2
print MyArray(i)
next i

sync
waitkey

makearray

function Test(anArray())
   anArray(0) = 10
   anArray(1) = 55
   anArray(2) = 987
endfunction



SpellSword

Ahh! I think I get it now.

If I understand this correctly, the array being altered isn't local to the function or global. Instead, it is being accessed from it's position outside the function from inside the function.
Change to example
I removed 'makearray' from your example when I ran it, it was producing the following error:
"Error in <Main.pba>,19: Syntax Error"


Thank you both for explaining that.
When I dream,
I carry a sword in one hand,
a gun in the other...

empty

QuoteInstead, it is being accessed from it's position outside the function from inside the function.
Yes, exactly.

QuoteI removed 'makearray' from your example when I ran it,
Oops. bad copy&paste remnant. :)