Omnimaga
Calculator Community => Major Community Projects => The Axe Parser Project => Topic started by: leafy on December 26, 2010, 09:58:19 pm
-
I still don't fully get how subroutines work - i've read the manual, but it doesn't explain much.
For instance, if you wanted a sprite to shoot an object and track the object with a subroutine, how would you do it? Or making a sprite jump in a platformer.
-
a subroutine can be thought of as just a chunk of code which you want to re-use in multiple parts of your program
say you wanted to open an appvar in multiple places in your program. you could use a subroutine, which would take a pointer to the appvar as an argument, and it would return a pointer to the appvar's data.
sub(APV,"appvONE")
//more code
sub(APV,"appvTWO")
Return //end the program
Lbl APV //this is the subroutine
GetCalc(r1) // r1 is the graphing variable.
Return //return from the subroutine
-
A subroutine is used for something that I need to execute many times during a code, like displaying a sprite.
. BUTTON X IS PRESSED
sub(AB
. BUTTON A IS PRESSED
sub(AB
Lbl AB
. DO STUFF
They are similar to labels, but a label makes the interpreter move to a certain position in the code, and a subroutine executes code stored in another position of the code (usually in the End)
-
So...I still don't get the difference between using subroutines and labels, and just labels and gotos. Are subroutines carried out simultaneously?
-
So...I still don't get the difference between using subroutines and labels, and just labels and gotos. Are subroutines carried out simultaneously?
no, nothing is simultaneous. a label just marks a place in a program. you can do GOTO LBL or Sub(LBL). both work. the difference is a GOTO will not return to where it was called. a Subroutine will.
Sub(LBL)
Disp "This is printed"
Goto LBL
Disp "this will not be printed"
Lbl LBL
Disp "printed twice"
Return
-
subroutines don't require you to know where to go back to, it's like creating a new command
-
Oh, okay. So if you wanted to code bullets being shot from a sprite, and there were multiple bullets, how would you keep track of them?
-
Oh, okay. So if you wanted to code bullets being shot from a sprite, and there were multiple bullets, how would you keep track of them?
storing the bullets as any number of bytes describing its direction, speed, position etc. and then putting them in a free ram area like L1
-
In short:
Labels jump to a certain part of your code. It's useful for things like menus -- use a goto, and imstamtly jump to the start of your progran (if that's what you want to do).
Subroutines are useful for reusimg code. For example, let's say that every time I press [2ND], I want to do somethimg complicated, then create a bullet. Instead of typing out the code that'll create a bullet every time I use the [2ND] key, I can just use a subroutine (which jumps to the subroutine, runs the code, then jumps back where you were) so that I have to type out the code only once. In addition to being more convenient, subroutines also save memory compared to writimg it out each time.