Page 1 of 1

Posted: Mon Dec 30, 2002 6:49 pm
by BackupUser
Restored from previous forum. Originally posted by pbdep.

Hiya All,

I dont seem to get it :wink:

How to return multiple values from a procedure?

Procedure test()
X = 1
Y = 2
ProcedureReturn(X, Y)
EndProcedure

Above fails, howto return multiple values?

Regards,
Norman.

Posted: Mon Dec 30, 2002 7:01 pm
by BackupUser
Restored from previous forum. Originally posted by pusztry.

To my knowledge you can only return 1 value from a procedure. As an alternative you could use a structure or an array. Arrays in PureBasic are always global so no need to even send it out just manipulate the data in the array and use it as you please. You can actually DIM the array in or out of the procedure.

Hope this helps

- Ryan


WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra

Posted: Mon Dec 30, 2002 7:01 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

> How to return multiple values from a procedure?

You can't -- you can only return one value from a procedure.

Workaround: Make the variables in the procedure global, so that they
retain their values when the procedure is done, and you also don't
need to use ProcedureReturn with them.

Posted: Mon Dec 30, 2002 7:12 pm
by BackupUser
Restored from previous forum. Originally posted by pusztry.
Originally posted by PB

> How to return multiple values from a procedure?

You can't -- you can only return one value from a procedure.

Workaround: Make the variables in the procedure global, so that they
retain their values when the procedure is done, and you also don't
need to use ProcedureReturn with them.
Wow we posted very close to the same time:)

- Ryan


WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra

Posted: Mon Dec 30, 2002 8:35 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

> Wow we posted very close to the same time

Posted: Tue Dec 31, 2002 12:20 am
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by pbdep

Above fails, howto return multiple values?
Use pointers:

Code: Select all

Procedure.l MultiReturn(*ret2.l)    PokeL(23, *ret2)    ; Second return value
    ProcedureReturn(45) ; First return value
EndProcedure

a.l = MultiReturn(@b.l)
You can just keep adding pointers, although if you have to have many returned values, you could create a structure to store the values you want to return and pass a pointer to it.

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)