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 / Applying Functions

Author
Message
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 8th Oct 2007 23:27 Edited at: 9th Oct 2007 01:03
Functions are very useful things. They are like GoSub but can have "variables" attached to it.
here is an example:

to call a function it is quite simple:

The above code will print hello world at the coordinates of 20 and 20.

so how do you apply this to your games?
Well there is several ways to!

This code will load and texture and place an object.


This one will delete an object from the screen:



as you can see there are many ways to use functions.
you can also make an inventory or declare items using arrays/types


functions are very easy to use. Make any requests on how to make something with functions on this post and i will help

Jeff032
17
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 9th Oct 2007 00:51
You should probably:
1) Indent the code
2) Make sure the code is correct, you want "endfunction" not "end function" which is in code snippets #1 and #3

Other than that I think it looks alright.

-Jeff

Space Game WIP
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 9th Oct 2007 00:52
Pretty cool, but I don't think functions are that useful when all they do is replace commands, i.e., your removeobject() one.

Perhaps you could also talk a little more indepth about parameters, and maybe also mention return values.

Some good pointers might also be that you can't send/return arrays to functions, unless they are global, and also, a very important and useful feature, local variables.

"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
HeavyAmp
18
Years of Service
User Offline
Joined: 25th Oct 2006
Location: Castle in the Sky!
Posted: 9th Oct 2007 03:28
You can send arrays to functions but not if the array is a User defined type. However you can send user defined types to functions but only if it is not in an array. Go figure

Better to be dead, than to live your life afraid.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Oct 2007 15:50
Quote: "Pretty cool, but I don't think functions are that useful when all they do is replace commands, i.e., your removeobject() one."

I do that all of the time. It allows you to do the job one way first, then do it another way later without having to change your main code at all. Or you may want to have your function carry out the basic task, but know that you want to extend it later (such as removing an object, then adding other code to do the internal game housekeeping).

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 9th Oct 2007 17:04
I agree with it if you intend to improve it ofcourse, but if you just do it to replace a command and not intend to change it then you are just wasting valuable speed. Also, if he did mean it that way, it probably would have been better to say so, so that people learning would understand better.

"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
Silicon Zombie
17
Years of Service
User Offline
Joined: 7th Oct 2007
Location: UK
Posted: 10th Oct 2007 01:18
I didnt want to make a new topic for this little Q, so Ill ask here since we are talking functions.

Why do alot of the tutorials say "always use functions instead of subroutines"? For me, I dont see the point of a function unless I want to pass parameters or get a return, I dont see anything wrong with the subroutines..is this advice given because you cant foldup subroutines to make your code neater, or is there some performance issue I am not aware of?
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Oct 2007 05:41
Quote: "I dont see the point of a function unless I want to pass parameters or get a return"


Neither do I - but I use DBC a lot more than DBP where you don't have global variables.

Procedures don't have the local variable problem and there isn't any difference speed-wise so it's down to personal choice.

You could write a program that uses no user-created functions if you wanted to - they are just more convenient in certain circumstances.

TDK_Man

Zergei
19
Years of Service
User Offline
Joined: 9th Feb 2005
Location: Everywhere
Posted: 10th Oct 2007 07:25
There is a difference in memory ussage.

Functions work with local variables. These local variables only exist in memory while the function is called, but once it finishes, the memory allocated to those variables is dumped.

To put it simple, if you have 10000 properly made functions, as soon as you execute your code, none of the local variables within those functions will be loaded into memory, nor memory space will be reserved for it, thus saving memory ussage.
However, replace those 10000 functions for subroutings, wich don't work with locals, and as soon as the program is executed, it will load all variables, whether they'll be used or not.

This is also the reason that is not so good to make lots of Globals, or at least not abuse of it.

Further on my stuff at...
TurboSquid.com
The3dStudio.com
Silicon Zombie
17
Years of Service
User Offline
Joined: 7th Oct 2007
Location: UK
Posted: 10th Oct 2007 15:28
Thanks for the replies, the books and tutorials never explained why they just said dont!

Login to post a reply

Server time is: 2024-11-25 12:35:05
Your offset time is: 2024-11-25 12:35:05