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.

Newcomers DBPro Corner / [Tutorial] Functions, subroutines, and where we can use them

Author
Message
calcyman
17
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 26th Oct 2007 14:02
Functions are best for elements that aren't specific to a single game. Drawing a filled-in circle would be best as a function (I've actually made that function for you) Subroutines should be used when you have chunks of code that you want to clear up.

To call a subroutine, use gosub label (where label is the title of the subroutine)

An example of a subroutine is here:



The subroutine is to make a waterwheel turn in a Lemmings game.

Let's analyse the main segments:



That tells us that it is the start of the subroutine.



That tells us to jump back into the main loop after being called.

For example, an FPS loop might have about 1200 lines of code, but it can be simplified to:



followed by a long list of subroutines.

You may learn how to make functions, but remember this:

A FUNCTION CAN ONLY READ THE VARIABLES YOU PASS TO IT.

If you don't pass a variable to it, it will not recognise it, and will see it as being a value of 0.

Try looking at my functions (in my #include tutorial) to help you understand the concept.

Your signature has been erased by a hyper-intelligent pan-dimensional being (a mod)
Jeff032
17
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 26th Oct 2007 14:14
Quote: "A FUNCTION CAN ONLY READ THE VARIABLES YOU PASS TO IT."


They can read global variables...

Space Game WIP
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 26th Oct 2007 14:35
Quote: "They can read global variables..."


And arrays dimensionised outside the function.
calcyman
17
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 26th Oct 2007 15:39
I didn't make this tutorial to attract criticism, and most newcomers won't use globals prior to learning functions. And it's bad practice to make a function that relies on globals, as it isn't very flexible (it can't easily be ported between source code)

Your signature has been erased by a hyper-intelligent pan-dimensional being (a mod)
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 26th Oct 2007 17:24
You havent mentioned returning variables, local variables, issues with functions (i.e. not being able to pass arrays, being able to pass UDT vars but not return them, etc), you havent compared functions to subroutines, speed-wise, or functionality-wise, and above that, you havent even shown us any code for functions.

Sorry for the critisism, but it's really needed. I think there's already a tut on functions anyway, but I'm not sure.

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
Jeff032
17
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 26th Oct 2007 18:25
Quote: "Quote: "And arrays dimensionised outside the function. "

And arrays dimensionised inside a function (same or other) if the GLOBAL keyword has been used."


Actually, you can access an array from anywhere in the code no matter where you dim it without using global. Unless you use the local keyword when dimensioning an array, it is automatically global.

Space Game WIP
Jeff032
17
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 26th Oct 2007 19:26
Strange, I had tested it and it worked fine. I'm using 6.6b.

Space Game WIP

Login to post a reply

Server time is: 2024-09-27 08:34:19
Your offset time is: 2024-09-27 08:34:19