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 / Functions!!! I don't understand them Please Help

Author
Message
LordDario
21
Years of Service
User Offline
Joined: 26th Apr 2003
Location:
Posted: 19th Apr 2004 03:01
Im pretty much new to programming in Dark Basic, and ive read some tutorials, and i read the Function Section in the Dark Basic Book, but im still a little confused with functions. I know how to declare a function and call upon one, but i don't understand the whole paremeter thing in a function. Could Anyone please Help me understand it. I usually understand code best in terms of Gaming lol. Thank you

Dario
Scouseknight
20
Years of Service
User Offline
Joined: 15th Mar 2004
Location: Bootle, Merseyside, UK
Posted: 19th Apr 2004 03:16 Edited at: 19th Apr 2004 03:18
Functions are like small programs in themselves that live inside your main program - they are rather like Subroutines.

Functions can just do something :



They can have parameters passed to them and use those parameters :



The most useful thing they can do though is work something out, and pass it back :



Functions are defined by Function <name> - and as soon as an EndFunction line is reached, the function is "closed".

Functions are called just by inserting the name of the function in your program.

Hope this helps.

CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 19th Apr 2004 04:11
think of a function as an oven that cooks a particular recipe (the code within), now picture the arguments or parameters as the "ingredients". You put the ingredients into the oven, which follows the recipe, and returns the cooked meal (sometimes slightly burned if youre not careful)


Home of the VB.NET Class Builder Utility - Demo and FULL versions now available!!
LordDario
21
Years of Service
User Offline
Joined: 26th Apr 2003
Location:
Posted: 19th Apr 2004 04:33
thanks, i think im understanding it a little better, im understanding how it works and what it does, let me put it to the test. Lets say i want to create a function for an rpg game that calculates damage dealt to a target in battle, is this right?

REM Calling Damage Function....

DamageCalculations(target1_attack,target2_defense,damage_dealt)

REM The Function itself

Function(target1_attack,target2_defense,damage_dealt)
damage_dealt=target1_attack-target2_defense
Print str$(damage_dealt)
EndFunction damage_dealt


Am i currectly using the functions in this case, or am i doing something wrong and how can i improve this?
Intruder
20
Years of Service
User Offline
Joined: 13th Apr 2004
Location: UK
Posted: 19th Apr 2004 04:53
Heres my take on the Function... Hope this is correct


** If GOD would have wanted us to walk around NAKED, he would have made us be born that way **
NathanF
20
Years of Service
User Offline
Joined: 24th Mar 2004
Location:
Posted: 19th Apr 2004 05:12
a very simple one to add numbers



.........what's the point of reading this?
LordDario
21
Years of Service
User Offline
Joined: 26th Apr 2003
Location:
Posted: 19th Apr 2004 05:53
oh ok i think i completely understand now, so you can set a variable equal to whatever the result of the function is. So in your example thirdnum will be returned with a result of 10, which then is saved into the variable result am i correct?
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 19th Apr 2004 06:10
yes, you got it now. A function can return itself or some other value from within, like "thirdnum" above.

one tip I might suggest is strongly type-ing the args - using the above as an exampe

function addition(firstnum as float, secondnum as float)
local fltSum as float
fltSum=firstnum+secondnum
endfuntion fltSum

(just using floats as an example, you args can be any valid variable type)


Home of the VB.NET Class Builder Utility - Demo and FULL versions now available!!
LordDario
21
Years of Service
User Offline
Joined: 26th Apr 2003
Location:
Posted: 19th Apr 2004 06:14
Gotcha thanks for your help and anyone else above who has helped, i feel i have a better understanding now, best thing to do now is practice thanks
NathanF
20
Years of Service
User Offline
Joined: 24th Mar 2004
Location:
Posted: 19th Apr 2004 06:50
Oh and another thing to remember is that variables from outside the function cant be used inside it, but arrays can.(and i think global vars can), hope that helps

.........what's the point of reading this?
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 19th Apr 2004 07:12
yes a variable declared as global can be used

Global intMyVar as integer = 1

ShowMyVar()
sync
wait key

Function ShowMyVar()
print str$(intMyVar)
EndFunction

this code should work and notice intMyVar was not passed in as an arg


Home of the VB.NET Class Builder Utility - Demo and FULL versions now available!!

Login to post a reply

Server time is: 2024-09-22 06:42:30
Your offset time is: 2024-09-22 06:42:30