Page 1 of 1

A simple question about returning an array from a procedure

Posted: Sun Feb 08, 2026 3:19 pm
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.

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

Posted: Sun Feb 08, 2026 3:26 pm
by Little John
How about looking at the documentation instead of asking ChatGPT? :wink:

https://www.purebasic.com/documentation ... dures.html

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

Posted: Sun Feb 08, 2026 3:30 pm
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.

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

Posted: Sun Feb 08, 2026 3:47 pm
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)

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

Posted: Sun Feb 08, 2026 3:52 pm
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)

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

Posted: Sun Feb 08, 2026 3:56 pm
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.