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 / Another Few Questions...

Author
Message
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 22nd May 2008 00:35
Hello guys, another few quick questions.
Question 1.
What's the point of functions? Is it just for neat code? Because I've noticed that when people use functions they don't usually use it to save time...?


Question 2.
Before signing up, I've looked through hundreds of those brilliant posts in the Dark Basic Classic Challenge Thread. All the time you guys (TDK Man, Nano Gamez, Obese) keep on saying, "Oh No! Don't use goto's use gosubs!" So I've switched to using gosubs... why is that? Does the program run faster with them?

Question 3.
Why use sync rate? Does it speed up the program? Or does it slow it down...?


Thank you very much everyone.

Syntax Error: Bob does not exist
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 22nd May 2008 01:35 Edited at: 22nd May 2008 01:37
Quote: "Question 3.
Why use sync rate? Does it speed up the program? Or does it slow it down...?"

sync rate lets you cap you sync speed for the game the orginal is set to 40 so say you want a faster refresh rate then set it to something like 60.

Quote: "Question 2.
Before signing up, I've looked through hundreds of those brilliant posts in the Dark Basic Classic Challenge Thread. All the time you guys (TDK Man, Nano Gamez, Obese) keep on saying, "Oh No! Don't use goto's use gosubs!" So I've switched to using gosubs... why is that? Does the program run faster with them?"


I'm not quite sure about this but I think that if you use goto in a loop to get a piece of code you have to use another goto again to jump back and it might not be the excate place either if you keep doing that you end up a code full of gotos(TDK Man calles it spaghetti code) at least I think thats why I don't quite remember as for gosub it's almost the same as goto but you get to use the return command so you jump back to the same place you were before you used gosub.

as for question 1 functions I've got no idea.

Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 22nd May 2008 01:37
Thank You very much ed! That was a lot of help! And yes, you need to use two gotos to go back, instead of return.

Syntax Error: Bob does not exist
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd May 2008 02:32
Question 1

A function is generally used to take a series of values, manipulate them and/or perform calculations on them, and then return a single value. A function is sort of like creating your own command that you can use over and over with a single call instead of writing the same bunch of code over and over. Also, functions use what are called LOCAL variables. That means the variables inside of the function only exist while the function is running. So, in terms of memory, when a function isn't running, it's variables don't exist so they don't take up any memory.

For example, let's say you need the result of adding numbers that change often as a program is running. You might create a function that handle that:



The values in between the parentheses are place holders that take input from the program and then run that input inside of the function. So, you could send different values at any time to the function and you will get back the result:



Quote: "Because I've noticed that when people use functions they don't usually use it to save time...?"


Functions do save time in that they allow you to change the input and get a different result without having to make unique code in every situation where values may be different. In terms of processing, it takes a little more porcessing resources to run a function than it does straight code, but the benefit is the function's flexibility and the elimination of redundant code from your program.

Question 2:
Just to add to what Ed222 said, GOTOs can be useful in a few situations but in general, they make code very difficult to follow because they JUMP around in the program with no identifiable path of return.
GOSUBs (stands for goto subroutine) are jumps that go to specific subroutines. A subroutine is like a function. It is a block of code that is initiated with a label (a string that identifies the name of the subroutine ending with a colon like MySubroutine: ) and it ends with the command RETURN. The RETURN jumps command back to the next line after the GOSUB was called. This helps manage the code very nicely. You can create subroutines that perform specific tasks like moving your character around or redrawing the screen, and GOSUB them every time you need it to happen. It's different that a function in that all it's variables are GLOBAL meaning they are in memory all of the time, and subroutines don't return any values.

Question 3:
DarkBASIC redraws the screen so you can see any changes at a certain rate. Bascially, there are a series of tasks that it will perform, maybe check for sounds playing, maybe look at file access, check for any 3d, update any loops etc. If you leave it up to the processor, it will prioritize all this stuff according to how it sees fit. For a game it might not be ideal, because the processor could be spending more time checking for math processing than it does redrawing the screen so things might end up jittery. This is where SYNC comes in.
SYNC forces the computer to redraw the screen at an interval/second that you choose. This can make the game run smoothly and consistantly. The maximum SYNC RATE that DBC refreshes at is 1000 frames per second. However, your CPU has to be capable of managing that. If you set the SYNC RATE to 0, then the screen will refresh as fast as it can. In general, setting the SYNC RATE between 40 and 60 gives pretty good results.

Enjoy your day.
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 22nd May 2008 02:58
thank you latch!

Syntax Error: Bob does not exist
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 22nd May 2008 07:02
Actually, you might find tutorials 1 to 4 in the thread I linked you to in your other thread quite useful. (It's a sticky at the top of this board).

They cover all this sort of stuff and somewhere in at least one of the tutorials you'll probably find the answer to 99% of all the questions you'll think of asking on here in the first few months of learning to program.

TDK_Man

Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 22nd May 2008 17:09
Okay, thanks TDK.

Syntax Error: Bob does not exist
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 22nd May 2008 17:10
ARGH! TDK! You make me feel EVEN MORE Noobish! MERCY!

Syntax Error: Bob does not exist
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd May 2008 01:56
It took me a very long time to see the point of making function, and even longer to know when to make one. Now I've got the hang of them I truly believe they are the best thing in programming!

At first gosubs and functions appear very alike, but as you learn you'll see how their differences make them suited to different tasks.
Gosubs are mainly used to make code easier to follow; by cutting the main parts of your program into separate routines (with explainative names) the reader can follow the processes of your program without having to read through every line of code.
Subroutines (posh name for gosubs) also add flexibility to your program; you can revisit the same gosub many times without having to retype it, your program no longer has to travel in a rigid straight line.
The only way of achieving this non-linear effect before was with the GOTO, but whereas gosubs allow your code to weave smoothly in and out, GOTO snaps it in half and crudely re-positions it with sticky tape.

Now onto functions...

It is far better to complete a 10 line program than to start a 10,000 line program.
Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 23rd May 2008 02:17
I owe you alot Obese! ;D

I'm not one of those Geeks who sit at home and eat potatoe chips and program games. I'm one of those wannabe's.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd May 2008 02:53
[Apologies for doubling but there's a character limit on PS3.]

Fuctions are much more independent from the main program than gosubs; he's like a maverick cop who doesn't play by the rules, he doesn't give a crap about global variables that those "standard procedure" guys use, he declares his own variables! He may be a loose cannon but he's the best goddamn cop in the city! No matter what the parameters, he always gets the job done.

Gosubs are like a doctor: He's consistent and always on call. He can examine and diagnose (modify) all global variables, but he can only treat parameters he has been trained on.
Sorry but gosubs are boring! But you wouldn't want a maverick doctor!

I thought it might help you learn faster if I made them into characters, there's a lot of real info hidden in there so hopefully it will help you to pick the right man for the job.

It is far better to complete a 10 line program than to start a 10,000 line program.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 23rd May 2008 14:41 Edited at: 23rd May 2008 14:44
The thing is, you need to have a balance of both procedures and functions in your programs. Use functions when you need them and procedures the rest of the time.

There are no real benefits speed-wise to either of them and they both do pretty much the same thing in slightly different ways.

A program which uses all procedures and no functions is OK. A program which uses all functions and no procedures is not so OK...

In my opinion, functions are more restrictive and less flexible than procedures - but please don't read that as saying 'not as good'.

For a start, in DBC, every variable you use in your main program cannot be seen inside a function so you have to pass a list of those you want to use in the function. Procedures however can see them already.

Likewise, everything you calculate inside a function cannot be seen by the main program, so you have to pass the result back. But you can only pass a single variable back (unless you use arrays which due only to a quirk in DBC act as global variables - if they worked as they should you wouldn't be able to do this).

With procedures, the main program can see any changes made to any variables in the procedure.

It sounds like I'm a bit anti-function, but that's not the case.

They can be very useful and allow you to do something that you can't do with procedures - create new commands. And to be honest, that's only because of the way that functions are called.

The DB command RGB() is a function. Three parameters are passed to it (the red, green and blue colour values) and one parameter is returned - the colour value.

If you need a single value calculating in this way then a function is what you need. And, as previously mentioned, the function can be used just like a native DB command.

If you create a function called MyFunc() you can use:

Print MyFunc(X,Y,Z)

just like you would:

Print RGB(128,128,128)

At the end of the day, local variables aside, there's nothing you can do inside a function that you can't do inside a procedure.

So, if you need to access or alter lots of main program variables then a procedure is probably better than a function.

If you need local variables which won't affect those with the same name in your main program and calculate just a single result then a function is better.

And one final thing in favour of functions... #Include files are meant only to include functions, so if you want to build libraries of often used code and place them in external files then they should be written as functions.

TDK_Man

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd May 2008 21:01 Edited at: 23rd May 2008 21:35
have a look at my structure tutorial
ill give it a bump so you can find it
@tdk have you read it yet? is it stickie material?

aah! its been auto-locked!
please have a look at it
it could very useful especially to newbies
im pleased with the planning section, its written in a much better style and is more valid than the first two sections. I'd like the chance to rewrite the first two sections and complete the tutorial.
I'm appealing to you because you are the most experienced tutorial author on here and will know if it's worth keeping.
It would be nice to contribute something to this community that has helped me so much.

here's the link
Structure Tutorial

It is far better to complete a 10 line program than to start a 10,000 line program.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 24th May 2008 02:14
I only comment on bad tutorials that give people duff info, so because I've not said anything about that one, you can safely assume I didn't find anything wrong with it!

Other than saying what is already in my tutorials that is.

Only kidding...

Unfortunately, even mods can't unlock auto-locked threads - I just tried.

TDK_Man

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 24th May 2008 02:46
thanks for trying
i guess it's doomed then

I can still edit posts though
how long have i got before it gets deleted?
is it possible to stickie an auto-locked thread?
if i completed it in time and to a high enough standard of course

It is far better to complete a 10 line program than to start a 10,000 line program.
Not_Maindric
17
Years of Service
User Offline
Joined: 10th Jul 2007
Location: Omaha, NE
Posted: 24th May 2008 04:05
Quote: "how long have i got before it gets deleted?"


"2nd Oct 2002 13:47"

Oldest thread in the DBC board. I think you have plenty of time to go.

bergice
18
Years of Service
User Offline
Joined: 5th Jun 2007
Location: Oslo,Norway
Posted: 24th May 2008 16:41
Functions save my life everytime i start a project.

For my guitar hero fangame i am using a "makenote(id,time)" function so i can make my own notes and specify their positions


But here is one thing i dont understand:

When i make about 2 or more functions in a program they always become so weird!
When i select text, it doenst highlight it and when i edit text, it doesnt make the changes before i scroll the program. :S
That is really annoying


Dont delete my sig mods, it is 600X120...
Slash!
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 24th May 2008 19:46
Quote: "When i make about 2 or more functions in a program they always become so weird!"


That's a problem with the IDE. If you haven't already done so, you should upgrade it. DarkIDE V1.56 is here:

http://darkbasic.thegamecreators.com/?m=showcase_view&i=65

TDK_Man

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 25th May 2008 22:21 Edited at: 25th May 2008 22:28
Quote: "I'm pretty sure he's using DBPro, so he should better install the unofficial update for the DBPro IDE instead."


..and stop posting on the DB Classic board!

Confusing all of us poor DBC users...

[Edit] Sorry - ignore this post completely. I thought WK was referring to the OP. My mistake...

TDK_Man

Login to post a reply

Server time is: 2025-06-06 17:34:13
Your offset time is: 2025-06-06 17:34:13