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 / what can go in a function

Author
Message
james1980
22
Years of Service
User Offline
Joined: 18th Sep 2002
Location:
Posted: 9th Nov 2003 21:05
I been away from dbp for about 6or7 months and all i can say is that i frogot a lot of stuff and my question is what can a function have in it i never understood functions.

2xP4 2.4 GHZ,2GB ocz ddr pc4000 ram, radeon 9800 128MB
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 9th Nov 2003 21:34 Edited at: 9th Nov 2003 21:41
a fuction is simply a block of code that accepts arguments, performs some action and returns a value to the calling code...

a typical function call:
myvariable=myfunction(arg1,arg2,...argN)

within a function you can use LOCAL variables that go out of scope when the function ends, and variables that are defined as GLOBAL in the top of your code.

(search the dbp help for uses and syntax)

Here is a function IanM wrote to replace dbp WrapValue function which breaks on values higher than 32000 or somewhere there-abouts

Function NewWrapValue(Source as float)
local Result as float
local Limiter as float
Limiter = int( Source / 360.0 )
Result = Source - (Limiter * 360.0)
if Result < 0.0 then inc Result, 360.0
Endfunction Result

If you wanted to use someother var in the function to help perform some action, aside from the LOCALS it needs to be dfined as a GLOBAL, but the return var and the calling var that is getting set does not need to be "global". It can be fltSomeVar as Float etc;

the call would look something like:
fltMyFloatVar=NewWrapValue(fltCurrentValue)

hope this helps...

-RUST-
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 11th Nov 2003 17:08
Salami. Salami can go in a function.

Oh. Wait now. Thats sandwich. Salami can go in a sandwich.

(sidles quietly off, embarrassed)

What do you mean, bears aren't supposed to wear hats and a tie?
Turoid
20
Years of Service
User Offline
Joined: 1st Nov 2003
Location: The Netherlands
Posted: 11th Nov 2003 19:18
Example:

this will make 2 cubes with a size of 100
Turoid
20
Years of Service
User Offline
Joined: 1st Nov 2003
Location: The Netherlands
Posted: 11th Nov 2003 19:20
ooops , this is the working example:
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Nov 2003 21:58
Functions are useful for code re-use. So you don't have to write long calculations multiple times.

function stuff(a, b, c, x, y, z)
distsq# = (a-x)^2 + (b-y)^2 + (c-z)^2
dist# = sqrt(distsq#)
endfunction dist#

Use in main program would look like this:

object_distance = stuff(10,-50,35.5,789,420,31.0) : rem function call

object_distance variable would now contain the distance between the two 3D points of the coordinates you plugged into the function call.
So instead of having to write out that equation multiple times, you can just plug in the 6 numbers and the rest is done for you. Might not seem like a lot a code saving, but imagine its usefullnes with much larger chunks of code. Functions can be pages long sometimes.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Nov 2003 23:48
LOL @ Philip. You are a very strange bear

Think of a function as a mini-program. It has all it's own variables and anything can go inside it - it just has access to the global variables too.

Login to post a reply

Server time is: 2024-09-21 09:23:57
Your offset time is: 2024-09-21 09:23:57