Menu

#NR Subroutine syntax

Help
John Mac
2008-02-04
2013-05-30
  • John Mac

    John Mac - 2008-02-04

    I have been hunting around for an explaination of what the #NR does in a subroutine call.  I found only a very short note in the GCBASIC Command Guide.  Says: #NR stands for No Return, and will stop the sub from returning values of variables.

    A little more explaination?

    Thanks
    John

     
    • Hugh Considine

      Hugh Considine - 2008-02-04

      Suppose that there is a subroutine:

      Sub SomeSub(Param1)
      Param1 += 2
      End Sub

      and it is called like this:

      MyVar = 2
      SomeSub MyVar

      By default, when the subroutine is called GCBASIC will copy the value of MyVar into the variable Param1. Then, when the sub exits, GCBASIC will copy the value of Param1 back into MyVar. So, after the call above MyVar will be equal to 4.

      Adding #NR after the name of the subroutine, like this:

      Sub SomeSub(Param1) #NR

      tells GCBASIC not to copy the value of the parameters back after the subroutine is done. So, with #NR used in the above code, MyVar would stay equal to 2 as it would not be changed by SomeSub.

      The advantage of this is that it stops GCBASIC from wasting the chip's time by copying things that don't need to be copied.

      One disadvantage of this method is that the #NR is applied to all of the parameters in the sub. In more recent builds of GCBASIC (those in update.zip), instead of adding #NR to the end of the sub definition, you can use In and Out before each parameter, like so:

      Sub MySub(In SomeParam, Out AnotherParam)

      With this code, GCBASIC will copy the value of SomeParam into the sub, but not out. It will also copy the value of AnotherParam out of the sub after the sub is finished, but will not copy the value in to begin with.

      #NR has no effect on arrays when they are passed as parameters. They are not copied as with byte and word variables, but rather GCBASIC passes the memory address of the array to the subroutine and the subroutine deals with it directly. Thus, the subroutine can change the array even with #NR.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.