Procedure arguments

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by talun.

Dear all,
I need a little help. I'm not able to use arrays in procedure arguments.
Can anyone help me?
Thankyou
Sergio
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

In Purebasic all arrays that's declared using 'Dim' are global. An array can't be passed as an argument to a procedure, but you can always access them from inside a procedure because, as i said earlier, they are global.

Just keep in mind that PureBasic is a one-pass compiler and as such have a few restrictions concerning in which order (line-wise) you Dim your vars and insert your Procedures.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

You pass arrays as a pointer by using ArrayName().

Code: Select all

Dim Array1.l(10)
  For a = 1 To 10 : Array1(a) = a               : Next a

Dim Array2.l(20)
  For a = 1 To 20 : Array2(a) = Random($FFFFFF) : Next a

Dim Array3.s(30)
  For a = 1 To 30 : Array3(a) = "Entry "+Str(a) : Next a

Structure LONG
  Long.l
EndStructure

Procedure ShowNumberArray(*Array.LONG,ArraySize)
   If ArraySize = 0: ProcedureReturn : EndIf
   For a = 1 To ArraySize
       *Array + 4
       A$ + Hex(*Array\Long)+Chr(13)
   Next a
   MessageRequester("Array numbers",A$,0)
EndProcedure

Procedure ShowStringArray(*Array.LONG,ArraySize)
   If ArraySize = 0: ProcedureReturn : EndIf
   For a = 1 To ArraySize
       *Array + 4
       A$ + PeekS(*Array\Long) + Chr(13)
   Next a
   MessageRequester("Array strings",A$,0)
EndProcedure


ShowNumberArray(Array1(),10)  ; pass Array1
ShowNumberArray(Array2(),20)  ; pass Array2

ShowStringArray(Array3(),20)  ; pass Array3
cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.

furthermore Shared array ; without the ()
supposed to gives access inside procedures
(why anyway ?)

Christos
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Some changes that perhaps make it a bit nicer to work with :)
Originally posted by Danilo

Code: Select all

Dim Array1.l(10)
  For a = 1 To 10 : Array1(a) = a               : Next a

Dim Array2.l(20)
  For a = 1 To 20 : Array2(a) = Random($FFFFFF) : Next a

Dim Array3.s(30)
  For a = 1 To 30 : Array3(a) = "Entry "+Str(a) : Next a

Structure LONG
  Long.l[0]
EndStructure

Procedure ShowNumberArray(*Array.LONG,ArraySize)
   If ArraySize = 0: ProcedureReturn : EndIf
   For a = 1 To ArraySize
       A$ + Hex(*Array\Long[i])+Chr(13)
   Next a
   MessageRequester("Array numbers",A$,0)
EndProcedure

Procedure ShowStringArray(*Array.LONG,ArraySize)
   If ArraySize = 0: ProcedureReturn : EndIf
   For a = 1 To ArraySize
       A$ + PeekS(*Array\Long[i]) + Chr(13)
   Next a
   MessageRequester("Array strings",A$,0)
EndProcedure


ShowNumberArray(Array1(),10)  ; pass Array1
ShowNumberArray(Array2(),20)  ; pass Array2

ShowStringArray(Array3(),20)  ; pass Array3


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

@Tinman
you have to change:
A$ + Hex(*Array\Long)+Chr(13)
to:
A$ + Hex(*Array\Long[a])+Chr(13)
and:
A$ + PeekS(*Array\Long) + Chr(13)
to:
A$ + PeekS(*Array\Long[a]) + Chr(13)

otherwise it doesn't work

Have a nice day...

Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by Franco
you have to change:
Doh - I went into autopilot there I think :)


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by talun.

Many thanks to all for the several suggestions. The global/shared array scope is handy for procedures located in the main program body, but the Danilo's trick is perfect for my needs of independent pieces of code.
Thanks!
Sergio
Post Reply