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.

2D All the way! / Goto/gosub/function

Author
Message
Code Stealer
21
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 9th Apr 2004 18:07
I have written a big piece of code to load up all my graphics. However, when I put it into sequence it looks really ugly. So, I have some questions about the gosub,goto and function commands. Any variables I generate in a subroutine arent used by the rest of the program unless I global them?

Also, how does the function command work? What is the syntax and Ive read that it "returns a value", but Im not sure quite what that means!

Ok, Ive included the piece of code, so I wonder if anyone could suggest the best way to include this code before I go into my main loop. Id like to get it in as a function, because that will look neatest! Thanks!

GIve me more power!
Emperor Baal
21
Years of Service
User Offline
Joined: 1st Dec 2003
Location: The Netherlands - Oudenbosch
Posted: 9th Apr 2004 18:16 Edited at: 9th Apr 2004 18:21
First of all, never use goto's, they make your code unreadable and unefficiently.

About your variable question. Gosubs can access variables that are not global. Example:


Output:
2

Functions use their own variables (unless they are global). Example


Output:
1


Returning a value when using a function is easy

Output:
2


About the mess, you can't do anything about it, you could try making it shorter by deleting unnecessary commands or putting the code in an external file (and linking it using the include command)

Quote: "
UPDATED

Amd 2800+ 1024mb pc3200 A7N8X - Deluxe Ati Radeon 9800PRO 256mb
"
Code Stealer
21
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 9th Apr 2004 18:32
Thanks!

I think Im going to use a subroutine and stash it well out ogf the way at the bottom of the code...

GIve me more power!
Emperor Baal
21
Years of Service
User Offline
Joined: 1st Dec 2003
Location: The Netherlands - Oudenbosch
Posted: 9th Apr 2004 19:59
That's a good idea. Another advice is to avoid small functions and gosubs. (I mean real small ones)

Example:

Output 3647 (3.647 seconds) on my machine


output 3669 (3.669 seconds) on my machine


output 3710 (3.710 seconds) on my machine


This looks like a tiny improvement, but every useless clock cycle is 1 clock cycle too many

Quote: "
UPDATED

Amd 2800+ 1024mb pc3200 A7N8X - Deluxe Ati Radeon 9800PRO 256mb
"
CosMind
21
Years of Service
User Offline
Joined: 13th Apr 2004
Location: Earth
Posted: 14th Apr 2004 03:23
does including external functions bog down processing speed?

it'd be great to just toss all functions in external files and include them in my source.

this way, i could easily update either or without having to make major overhauls to the entire code - plus, it would prevent me from having to wade through a massive code base just to tweak singular snippets...

Login to post a reply

Server time is: 2025-08-09 02:12:44
Your offset time is: 2025-08-09 02:12:44