Returning an array from a procedure

Just starting out? Need help? Post your questions and find answers here.
jcoggins
User
User
Posts: 25
Joined: Sat Apr 12, 2008 12:30 am

Returning an array from a procedure

Post by jcoggins »

Is there a way to have a procedure return an entire array?

Jason
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

No, you must pass the array as a parameter, and then let the procedure fill it in.
jcoggins
User
User
Posts: 25
Joined: Sat Apr 12, 2008 12:30 am

Post by jcoggins »

Can anyone provide a simple example?

Jason
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Code: Select all

Procedure MyProc(MyArray.l(1)) ;1 in brackets is the number of dimensions
    MyArray(5) = 100
    MyArray(10) = 1000
EndProcedure

Dim AnArray.l(20)
AnArray(5) = 10

Debug AnArray(5)
Debug AnArray(10)

MyProc(AnArray())

Debug AnArray(5)
Debug AnArray(10)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

EDIT: Removed because of nonsense. :lol:
Proud registered Purebasic user.
Because programming should be fun.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

still preoccupied with returning a structure? :twisted:

Edit, I can't keep up with your edits Byo!! :shock:
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You can create an array in a procedure and return its address via ProcedureReturn @MyArray() with one proviso - the array must be created with a persistent scope, e.g. Global or Static else it's gone after the procedure finishes.
BERESHEIT
Post Reply