News:

Let's find out together what makes a PIC Tick!

Main Menu

Procs with parameters passed as ByRef

Started by JohnB, Mar 04, 2024, 06:35 PM

Previous topic - Next topic

JohnB

When a parameter is passed ByRef to a procedure which the goes on to modify that parameter, should I expect it to appear modified when next read after executing the procedure?

e.g.
Dim AParam As SWord
 
Proc MyProc(ByRef MyParam as Sword, IncVal as byte)
  Inc MyParam,IncVal
EndProc

AParam = 50
MyProc(AParam,2)
HrsOut Dec AParam

In the example should AParam hold the value 52 or does it only apply to variables within the Proc

JohnB

top204

#1
With the ByRef directive you will need to use one of the PtrX commands to read and alter an indirect variable passed by address alone. What size Ptr is used depends on the type of variable the address represents. The variable type after the ByRef parameter is there so the compiler knows what size variable to create and use to hold its address, not what the address represents.

So your code will look like:

'-------------------------------------------------------------------------
' The main program starts here
'
Main:

    MyWord = 50
    HRSOutLn "MyWord = ", SDec MyWord
    MyProc(MyWord, 2)
    HRSOutLn "MyWord After Increment = ", SDec MyWord
    Stop
   
'-------------------------------------------------------------------------
' Increment a 16-bit signed variable, who's address is passed
' Input     : pParam1 holds the address of the variable to increment
'           : pIncVal holds how much to increment by
' Output    : The 16-bit signed variable who's address was passed in pParam1 will have been incremented
' Notes     : Uses indirect methods using the Ptr16 command to read and write the variable, because the variable to increment is a word size
'
Proc MyProc(ByRef pParam1 As Word, pIncVal As Byte)
    Dim wTemp As SWord                              ' Create a temporary signed word variable

    wTemp = Ptr16(pParam1)                          ' Read the 16-bit variable who's address is held in pParam1
    Inc wTemp, pIncVal                              ' Increment the signed temporary variable
    Ptr16(pParam1) = wTemp                          ' Write the 16-bit temporary variable's contents to the address passed in pParam1
EndProc

On the serial terminal with the above program running, it will display:

MyWord = 50
MyWord After Increment = 52


At the time of writing, I could not come up with a way to set a size for a parameter passed ByRef within the compiler's list structures and variable assignments and comparisosns etc, but I have been thinking of ways, so the variable passed by reference can be automatically known to a Ptr command, based upon its following type after 'As'. I will try and implement this method over time, so it keeps backward compatability, which is very important, even though it adds a lot more complex coding within the compiler's parser and mechanism. :-)

JohnB

That makes a lot of sense now. Perhaps you could add this as a second example in addition to that shown in the manual.
JohnB