Author Topic: recursive subroutines-Bad, Not recommended or neutral?  (Read 6734 times)

0 Members and 1 Guest are viewing this topic.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
recursive subroutines-Bad, Not recommended or neutral?
« on: December 23, 2010, 12:47:02 am »
I have a program that would be much easier to make if I use a recursive subroutine. What I mean by this is Inside of one subroutine, there is another subroutine that references the first subroutine inside of itself. Is that bad? I can think of a way around it, but it makes the code oh so much less readable and understandable, since there will be an End before its connected While, and that too may throw errors XD

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #1 on: December 23, 2010, 12:58:25 am »
In Axe or ASM, it's not bad as long as you ALWAYS return from each subroutine by the end of the code. If you don't, then you're going to crash your calc.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #2 on: December 23, 2010, 12:59:46 am »
yeah, but now it's not letting me compile. It might be something else, but ergh.

So a safeguard could be just having several Returns at the end of the program, right?

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #3 on: December 23, 2010, 01:01:39 am »
No. Too many returns will also crash your calc. One subroutine call, one return. If they do not match up, you're probably going to clear your RAM. Axe might not want to compile because it doesn't see that you're calling the routine more than once before returning.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #4 on: December 23, 2010, 01:06:04 am »
Perhaps. Having a while before an end also might cause problems, depending on how it detects proper block setups. The issue is, it's currently structured like so:

lbl Main Sub
Blah
Blah Blah
If ...
sub(other
end
Return

lbl other
blah
blah blah
Repeat Getkey(x)
sub(Main)
end
blah blah
Return

The If that triggers the "other" sub will not be triggered again until after the sub has reset itself (after the repeat's End) , so....it should work.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #5 on: December 23, 2010, 01:07:29 am »
Recursive subroutines are great and often reduce the size of the program a lot.  The drawback is that they are generally a little slower and there is a limit to how many nested subroutines and pushed variables you can have before you get a stack overflow which crashes the calc.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #6 on: December 23, 2010, 01:08:09 am »
I see a lot of potential for screwing up the stack with that structure. Could you give us some context on why it would be difficult to do it any other way?
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #7 on: December 23, 2010, 01:08:55 am »
Too many returns will not crash your calc, but it will not fix the issue either.  The only way to make sure that your calc doesnt crash is to make absolutely sure that you return all the way out of every subroutine.  As long as you don't recurse infinitely into the same subroutine, and as long as you don't call it over 9000 times, just put a return at the end of your subroutine and you should be perfectly fine.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #8 on: December 23, 2010, 01:10:05 am »
Don't worry, right now I can afford a little speed loss. Also, as of right now I see a max of 3 nests, meaning 4 subroutines at once :P And there are going to be few variables. This is for my battle Engine, BTW. The subs are recursive so it can wait for a buttonpush to allow you to attack while time still increases, to allow for double and triple techs between party members. Fortunately, monsters will not need this setup.

Edit: Double ninja :P

Like I mentioned, I could also do it with a while loop, but it would be much more complicated, and therefore be more buggy :P

By the way, the if that triggers said "other" subroutine is a direct part of that subroutine's function, and, if written without glitches, it should function exactly as intended.

Want some context?

Battle starts, time starts going up. One person reaches full time, it triggers the If, and goes to the "other" sub with a few vars set. The "other" sub then reactivates the main Time Increment subroutine so that other players can have their time increase, but it does so while checking with a Repeat Getkey for 1, 2, or 3 to be pressed, respective to the player whose time is full. The If is an = check, so once time goes over with the next increment it will not be triggered for the same player until their "Other" loop resets their time to 0.
« Last Edit: December 23, 2010, 01:15:57 am by willrandship »

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #9 on: December 23, 2010, 01:10:56 am »
No. Too many returns will also crash your calc. One subroutine call, one return. If they do not match up, you're probably going to clear your RAM. Axe might not want to compile because it doesn't see that you're calling the routine more than once before returning.

too many returns would crash your calc if you were, say, trying to calculate the 50th fibonacci number recursively.

willrandship, as long as that If statement won't trigger while lbl other has been called, you're fine.


Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #10 on: December 23, 2010, 01:12:27 am »
Too many returns will not crash your calc, but it will not fix the issue either.

Doesn't Axe Push PC onto the stack, then pop it with Return? If that's the case, then too many returns will pop a random byte into PC, which may or may not contain valid code.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #11 on: December 23, 2010, 01:14:08 am »
Too many returns can't crash your calc, too many calls can however.  Like for example:

Code: [Select]
Lbl Sub
Disp "HI
Return
Return
Return
Return

Calling this subroutine won't crash because only the first Return will be experienced.  No matter how many returns you have, at the very *most* you will return to where you started, which is a safe place.  The only ways a crash can happen is if you call too many times and run out of stack space, or if you don't return enough and end up in a weird place

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #12 on: December 23, 2010, 01:16:05 am »
Oh, I'd forgotten about that. One of the optimizations in the documentation is to not add a Return to the end of the program :P
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #13 on: December 23, 2010, 01:17:40 am »
Oh, I'd forgotten about that. One of the optimizations in the documentation is to not add a Return to the end of the program :P

that's an auto-optimization already.


Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: recursive subroutines-Bad, Not recommended or neutral?
« Reply #14 on: December 23, 2010, 01:21:55 am »
Its actually not auto optimized anymore due to the possibility of memory leaks i believe in included programs