Return more than one variable?

Just starting out? Need help? Post your questions and find answers here.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Return more than one variable?

Post by GeoTrail »

When using ProcedureDLL or just Procedure, can it return more than one variable?

Like this:

Code: Select all

ProcedureReturn Result1$,Result2$
?
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

No, but you can use Structures to return multiple values:

Code: Select all

Structure test
  a.l
  b.l
EndStructure

Procedure test(*ReturnMe.test)
  *ReturnMe\a = 42
  *ReturnMe\b = 23
EndProcedure

TestMe.test
TestMe\a =0815
TestMe\b = 4711

test(TestMe)

Debug TestMe\a
Debug TestMe\b
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

OK, thanks for that :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: Return more than one variable?

Post by einander »

GeoTrail wrote:When using ProcedureDLL or just Procedure, can it return more than one variable?
Having 2 small arrays as return buffers, you can return numbers or strings easily.

Code: Select all

;Return multiple values from Procedure

Dim Buff.L(10):Dim Buff$(10)  ; Return buffers

Procedure.s DoSomething() 
  Buff(0)=111:  Buff(1)=222  : Buff(2)=333       
  Buff$(0)="Result$ Zero" : Buff$(1)="Result$ One" :Buff$(2)="Result$ Two"
  ProcedureReturn "Here are 6 returns from Procedure DoSomething:"
EndProcedure

Debug DoSomething()

For i=0 to2
  Debug Str(buff(i))+"  "+ buff$(i)
Next
Best regards
Einander
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Wow, that looks complicated. Haven't gotten that far in learning PB yet ;)
Thanks anyways :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

why cant you use:

procedure dothis()
var1 =
var2=
procedurereturn var1 and var2
endprocedure

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

AND
?

Will that work? Has anyone tried that?
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

No, this won't work.

Timo
quidquid Latine dictum sit altum videtur
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

GeoTrail wrote:Wow, that looks complicated. Haven't gotten that far in learning PB yet ;)
Thanks anyways :)
GeoTrail,

It's not that complicated... In PureBasic arrays are always global. So if you update an array element inside a procedure, it's value will be available anywhere in your program.

HTH,
blueb
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i sometimes do use globals as well to return values from procedures

it all depends on what you do with them, see, if i write one or more procedures that happen to do related stuff, they might share some information... in those caes, i use some specific variable names that are a. unlikely to be used elsewhere, and b. global, and c. shared between the different procedures and have similar functionality

for example:

Code: Select all

Procedure.l x_exist(filename.s)
  Global x_creation.l, x_lastaccess.l, x_lastwrite.l, x_filesize.l
  Protected st.SYSTEMTIME
  ;
  ; *** check if a file exist and if so, retrieve information on a file
  ;
  ; in:     filename.s               - path + filename
  ; retval: 1 #True                  - found and info retrieved
  ;         0 #False                 - not found or cannot get my greedy fingers on it
  ; out:    x_filesize.l             - size of file
  ;         x_creation.l             - when created
  ;         x_lastaccess.l           - when last accessed
  ;         x_lastwrite.l            - last written To
  ;
  file_h = CreateFile_(@filename.s,0,0,0,#OPEN_EXISTING,0,0)    ; open only for retrieval of attributes
  If file_h <> #INVALID_HANDLE_VALUE                            ; ok it exists
    x_filesize.l = GetFileSize_(file_h,0)                       ; that's the size
    ;
    creation.FILETIME                                           ; create temp vars  
    lastaccess.FILETIME
    lastwrite_ft.FILETIME
    st.SYSTEMTIME
    ;
    GetFileTime_(file_h,@creation,@lastaccess,@lastwrite)
    ;
    x_creation.l = x_filetimetodate(@creation)
    x_lastaccess.l = x_filetimetodate(@lastaccess)
    x_lastwrite.l = x_filetimetodate(@lastwrite)
    CloseHandle_(file_h) 
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure
all the procedures in my personal library use the following naming scheme: x_whatever... so if i need to return more than one thing from a procedure, i either use x_procedurename_whatever if it's meant to be shared between procedures but i normally don't touch it, or i use x_whatever to return additional information

for example, the procedure above returns x_creation with creation time and date of the file, x_lastaccess, and x_lastwrite

of course it's essential to document this kind of usage, and to declare those variables global
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

ok, so if in a LIB i write with tailbite, i want to create a command that gets an MDI gadget properties and store them in a structure.

basically i want the user to be able to set values for an x, y, width height of an MDI gadget when a window is in a docked position, and then when the window is undocked, the MDI automaticlly resizes to predefined x,y,width,height values in the undocked state.

Code: Select all

Structure dockedproperties
  MDIWidth.l 
  MDIHeight.l 
  MDIdockedx.l
 MDIdockedy.l
EndStructure 

Structure undockedproperties
  MDIWidth.l 
  MDIHeight.l 
  MDIdockedx.l
 MDIdockedy.l
EndStructure 

proceduredll dockedproperties(MDIgadget);this will set the efined values of above structures
endprocedure

proceduredll undockedproperties(

endprocedure

proceduredll flipdockstate(dockhandle,MDIgadget,@docked/undockedproperties)
resizegadget(MDIgadget,----structure here)
endprocedure


this way, quick undocking or docking a window automatically resizes the MDI to values defined in the procedures. basically i want to the user to be able to setup these values just by using the procedures and then flipping the "dock" state.

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

If you only have one docked window, I would store the data in a global structured variable. Otherwise, I would use a structured linked list (if using TailBite, remember to declare the list in a procedure).
El_Choni
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

can you give an example. i thought variables in a library or DLL cant be accessed in PB code?

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Would using Global makes a variable in a dll sortof-kindof public? Ditto in a tailbite lib?

Can't test this myself right at the mo.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Yes, you can, by providing a pointer to the variable. But a more clean solution is to give a function that retrieves that value like, for example, WindowID() XD

That is, your DLL/lib code accesses the variable/linkedlist/memory block, and returns it to the main code.

Using Global makes your variable permanent in memory, and public to the same process (even the program calling the DLL/lib) but not by name, but by pointer. Locals are only in the procedure stack.
El_Choni
Post Reply