News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Variable return procedure

Started by Giuseppe MPO, Aug 26, 2021, 11:12 PM

Previous topic - Next topic

Giuseppe MPO


Hi all,
I'm trying to understand and use the procedures, a question,
but the procedures can receive up to 10 variables but can only return one?
Thank you all

John Drew

#1
Hi Giuseppe,
Yes, you're right but the easy way around is to use global variables inside the procedure. It doesn't cost in extra RAM space. Another would be to use pointers.
John

RGV250

Hi,
I am not sure if it is possible with procedures but I recall using an array as a return variable many years ago, not sure of the circumstances now as it was years ago or if it is possible with a procedure.

Regards,
Bob

trastikata

Hello Bob, see page 40 in the manual.

top204

#4
There are a few ways to return several variables from a procedure. The one used by most languages that support pointers, is to return the variable, as John stated, as a Pointer. Which means the parameter is created as ByRef, so it holds the address of the variable being sent to the procedure. Then using one of the PtrX functions to get the original value (if there is one) then loading the parameter's address with the value to return, using a PtrX function. In most languages it is called Pointer manipulation, but I like to call it its "real" name and I call it By Address.

Another way is: If the returns required are Byte or Word sizes, return a Dword, so it can contain 4 or 2 variables. i.e. Byte0, Byte1, Byte2 and Byte3 or Word0 and Word1. Or a mixture of them. Then create alias names to the return variable's pieces for clearer code reading.

Another way, as stated above, is to return an Array, so each element of the array holds individual values. Very simple and very neat, and aliases can be created for an Array, so the names can be clearly understood. Also, the compilers have the "unique" ability to access each element of an array as individual variables. In Positron8: MyArray#X. In Positron16: MyArray_X.

This is, in its simpler form, the way a structure works is some languages, and they are something I am hoping to look into adding at some point in the future, once our life is settled back home in North-West Norfolk sometime in the, hopefully, near future. :-)