A simple question about returning an array from a procedure

Just starting out? Need help? Post your questions and find answers here.
ZX80
Enthusiast
Enthusiast
Posts: 404
Joined: Mon Dec 12, 2016 1:37 pm

A simple question about returning an array from a procedure

Post by ZX80 »

Hi all !

The question is in the topic title.
The point is that the array is multidimensional !
Sorry if this is a stupid question.

If you write it like this (as we are used to doing for a one-dimensional array):

Code: Select all

Procedure SomeProc(Array try(1,1))
, it will be a mistake. By the way, chatgpt suggested me this code as a solution to the problem.

Further...
chatgpt suggests this to me (via poke and peek):

Code: Select all

Procedure.i Create2DArray(width, height)
  *mem = AllocateMemory(SizeOf(Integer) * width * height)

  For x = 0 To width - 1
    For y = 0 To height - 1
      PokeI(*mem + (x * height + y) * SizeOf(Integer), x * 100 + y)
    Next
  Next

  ProcedureReturn *mem
EndProcedure

width = 3
height = 4

*matrix = Create2DArray(width, height)

For x = 0 To width - 1
  For y = 0 To height - 1
    Debug PeekI(*matrix + (x * height + y) * SizeOf(Integer))
  Next
Next

FreeMemory(*matrix)
Maybe there is a better version ?

Thanks in advance.
Little John
Addict
Addict
Posts: 4852
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: A simple question about returning an array from a procedure

Post by Little John »

How about looking at the documentation instead of asking ChatGPT? :wink:

https://www.purebasic.com/documentation ... dures.html
ZX80
Enthusiast
Enthusiast
Posts: 404
Joined: Mon Dec 12, 2016 1:37 pm

Re: A simple question about returning an array from a procedure

Post by ZX80 »

Little John, thank you.

I also considered and tried a structured array as a procedure parameter.

Code: Select all

Procedure LoadGeoidFile(Filename$, Array try.Word(1))
  Protected.i i, j
; ...
  
  try(i, j)\w = ((*Ptr\w & $FF) << 8) | ((*Ptr\w & $FF00) >> 8)
  
; ...
  ProcedureReturn resultEn
EndProcedure

Dim GeoidData.w(#i, #j)

LoadGeoidFile("WW15MGH.DAC", GeoidData())
The compiler reports that the last parenthesis is missing.
User_Russian
Addict
Addict
Posts: 1629
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: A simple question about returning an array from a procedure

Post by User_Russian »

ZX80 wrote: Sun Feb 08, 2026 3:19 pmIf you write it like this (as we are used to doing for a one-dimensional array):

Code: Select all

Procedure SomeProc(Array try(1,1))

Code: Select all

Procedure Test(Array x(4))
  x(0, 0, 2, 0) = 1234
EndProcedure

Dim a(10, 10, 10, 10)
Test(a())

Debug a(0, 0, 2, 0)
infratec
Always Here
Always Here
Posts: 7803
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A simple question about returning an array from a procedure

Post by infratec »

Code: Select all

Procedure.i LoadGeoidFile(Filename$, Array try.w(2))
  
  try(1, 2) = 666

  ProcedureReturn resultEn
  
EndProcedure

#i = 719
#j = 1439

Dim GeoidData.w(#i, #j)

LoadGeoidFile("WW15MGH.DAC", GeoidData())

Debug GeoidData(1, 2)
ZX80
Enthusiast
Enthusiast
Posts: 404
Joined: Mon Dec 12, 2016 1:37 pm

Re: A simple question about returning an array from a procedure

Post by ZX80 »

Peter, thank you very much ! That was my mistake. The array dimension should be in parentheses, not the size. But with another code it causes an error.


Added:

infratec, thank you. My bad.
It turns out I made another mistake by writing '.Word' as an array structure. :oops:
I have already realized my first mistake. Apparently we were typing at the same time.
Apparently I was hasty. Thanks for the correction.

P.S. 666 - I hope it's not me.
Post Reply