Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: leafy on February 03, 2011, 01:21:43 am

Title: Subroutine Argument overwriting
Post by: leafy on February 03, 2011, 01:21:43 am
If you call a subroutine inside a subroutine with arguments of its own, will it overwrite the arguments of the original subroutine? For example,

sub(GRV,A,B,C)

Lbl GRV
sub(SE,D,E,F)
Return

Lbl SE
some other code

Since they both depend on r1 to r6, will the variables get overwritten?
Also, if you have something like sub(GRV,X,Y) and you want to save the value created in the subroutine back into Y, does the subroutine do that automatically? Like

Lbl GRV
r2+1 -> r2
Return

Will Y get added an increment of 1?
Title: Re: Subroutine Argument overwriting
Post by: Builderboy on February 03, 2011, 01:31:50 am
So the only thing that happens when you call a routine with arguments, is that the numbers you put into the sub(LBL,#,#,#) get *copied* into r1-r6.  When the numbers are copied into r1-r6, anything inside of the r1-r6 is overwritten.
 r1-r6 are completely independent variables from A-Z, and can be used the same way.
Title: Re: Subroutine Argument overwriting
Post by: Michael_Lee on February 03, 2011, 02:16:37 am
Normally, becaus r1 through r6 are global vars, their value gets overridden every time a subroutine using those are called.

Axe provides a way around this -- tack a radian sign after the name of the subroutine, and the values of r1 to r6 will be restored after that subroutine is finished.  Personally, I'm a little leery of using that (even if it does allow for more recursiveness, always a plus) because the values of r1 to r6 are saved to a stack, and I don't know how much memory that stack can hold before something bad happens.

To actually change the value of the variable you loaded, pass a pointer.
Code: [Select]
.TEST
5->Y
sub(ABC,<degree sign here>Y)
ClrHome
Disp Y>Dec
Pause 2000
Return

Lbl ABC
{r1}r+1->{r1}r
Return
The value of Y should be changed to 6.
Note: not tested, but should work.  Prefix a variable with the circular degrees symbol to return a pointer to it.
Title: Re: Subroutine Argument overwriting
Post by: Quigibo on February 03, 2011, 03:47:29 am
Using recursive subroutines with the r modifier only backs up the variables it uses so if you mess with the other r1-r6 that are not passed as arguments, there values will be changed as well.  So calling:

:sub(A,50)
:
:Lbl A
:Disp r1>Dec
:sub(B,100)
:Disp r1>Dec
:Return
:
:Lbl B
:Disp r1>Dec
:Return


Prints: 50,100,100

:sub(A,50)
:
:Lbl A
:Disp r1>Dec
:sub(Br,100)
:Disp r1>Dec
:Return
:
:Lbl B
:Disp r1>Dec
:Return


Prints: 50,100,50 since r1, and only r1, is restored after the call.
Title: Re: Subroutine Argument overwriting
Post by: Michael_Lee on February 03, 2011, 02:52:44 pm
Out of curiosity, Quigbo, how deep can I nest recursive subroutines before it starts flipping out?