Author Topic: Subroutines  (Read 6700 times)

0 Members and 1 Guest are viewing this topic.

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Subroutines
« 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.
In-progress: Graviter (...)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Subroutines
« Reply #1 on: December 26, 2010, 10:03:27 pm »
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.
Code: [Select]
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


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Subroutines
« Reply #2 on: December 26, 2010, 10:04:36 pm »
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)

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Subroutines
« Reply #3 on: December 26, 2010, 10:06:35 pm »
So...I still don't get the difference between using subroutines and labels, and just labels and gotos. Are subroutines carried out simultaneously?
In-progress: Graviter (...)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Subroutines
« Reply #4 on: December 26, 2010, 10:08:37 pm »
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.

Code: [Select]
Sub(LBL)
Disp "This is printed"
Goto LBL
Disp "this will not be printed"
Lbl LBL
Disp "printed twice"
Return
« Last Edit: December 26, 2010, 10:09:14 pm by nemo »


Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6266
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: Subroutines
« Reply #5 on: December 26, 2010, 10:10:34 pm »
subroutines don't require you to know where to go back to, it's like creating a new command
/e

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Subroutines
« Reply #6 on: December 26, 2010, 10:28:43 pm »
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?
In-progress: Graviter (...)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Subroutines
« Reply #7 on: December 26, 2010, 10:32:30 pm »
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


Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Subroutines
« Reply #8 on: December 26, 2010, 10:33:30 pm »
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.
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.