Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Ladders, Elevators, Escalators.

Author
Message
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 21st May 2011 09:04
Subroutines are like Lego blocks for your programs. If you want to build a small plastic house, you could go straight forward with it and buy plastic, melt it down, pour it into a mold, and then you have a house (alternatively you could carve it out of a block or use a rapid prototyping machine, but that's beside the point). This is akin to programming without using subroutines. The end product is solid, but you can't reuse or reshape it.

Alternatively, you can use legos: modular, small pieces that you assemble in the appropriate order to make what you want.

Your finished program is like the house, your code, the building method. You could just start typing away, repeating code every time you need to do it. It will run, though not efficiently or cleanly at all. Or, you could use subroutines.

Subroutines are like the lego bricks of the program. They are small, modular pieces of code that you call in the right order to accomplish something. You call subroutines using the GOSUB command. Generally, you should use a subroutine if you end up using a particular section of code more than once. You would just put it at the end of your program, give it a label (whatever you want, be clear and specific as to what the subroutine does) which is something to the effect of "HelloWorld:"
and at the end put a RETURN command. The RETURN command will return you to the line immediately following the GOSUB command. This is very similar to the old, obsolete, and completely frowned upon command: GOTO, which worked the same way, it just didn't have support for the RETURN command (it's an easy habit to get into using GOTO's, but the nearly universal consensus is: don't).

Other tricks with subroutines are to remove any constants and swap them out for variables (within reason). Then before the GOSUB call you can change the variable values and make the subroutine work on a different sprite, bitmap, object, etc. If you want to do this, though, it is usually easier to just write a function (similar to a subroutine, but slightly more complicated).

Does that answer your question? Or did I just confuse you more?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
DarkBasicBegin
12
Years of Service
User Offline
Joined: 27th Apr 2011
Location:
Posted: 21st May 2011 20:19
Yeah I'll pass on adding those :o
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 21st May 2011 21:22
Actually, subroutines really are the first hurdle to being able to program really cool things, because they let you branch your program.

They work kinda like this:


See how even in the loop I can use them to go to different places? Now if I wanted too, I could keep the subroutines just as they are, but change the main loop code so that one button will always make the box red, and another will always make it blue.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
DarkBasicBegin
12
Years of Service
User Offline
Joined: 27th Apr 2011
Location:
Posted: 22nd May 2011 23:44
So in the do loop i would put somewhere gosub nextlevel
then have like
Nextlevel:
If object collision (-,-)
clearworld
etc
etc?
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd May 2011 00:11
You could do that, my initial reaction would be put the check for collision in your main loop for clarity sake, then within that if, just put a GOSUB. Your NextLevel routine looks good (in theory). Clear the current world, then load the next, then go back to the normal main loop. But yeah, you seem to have the right idea.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose

Login to post a reply

Server time is: 2024-03-28 20:38:02
Your offset time is: 2024-03-28 20:38:02