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 Professional Discussion / Questions Concerning The Capibilites Of Functions

Author
Message
Rwilson
23
Years of Service
User Offline
Joined: 19th May 2003
Location: United States
Posted: 24th Aug 2003 23:36
Question 1: Is there a way to return multiple values from a Function? I realize that most only want to return one number, but take this for example:

Function AttackMath(Str,Endur)
Str+Endur=Attack
EndFunction Attack

Function MagAttackMath(MagPow,Str)
MagPow+Str=MagAttack
EndFunction MagAttack

Function SpeedMath(Dex,Endur)
Dex+Endur=Speed
EndFunction Speed

Right now, I have about 9 of those little mini-functions. I'd like to cram them all togehter into something like this:

Function StatMath(Str,Dex,Endur,MagPow)
Str+Endur=Attack
MagPow+Str=MagAttack
Dex+Endur=Speed
EndFunction Attack,MagAttack,Speed

According to my father, who used Visual Basic, from version 4 to 7, such a thing was possible, and the DBpro help also mentions being able to return multiple numbers (Quote:They can accept multiple parameters and return values). However, I am lost as to how one can go about doing so, for DBPro dosn't like the ways I've attempted to do so.

Question 2: Can I pass an Array into a function like so:

Function PrimStatFunc(PrimStats)
PrimStats(1,1) + PrimStats(1,2) = return
EndFunction Return

This question is more out of curiousity than necessity. While a confirmation on it's worlabiltiy would allow some simplification of code, I would do fine without.

Question 3: Can I modify a User Defined Type Array from within a Function, like so:

Type DaDa
Hel AS INTEGER
Bello AS STRING
Gella AS Integer
EndType

Dim Array AS DaDa
Array.Hel=0
Array.Bello=""
Array.Gella=0

Perform (Array)

print Array.Hel
Print Arra.Bello
Print Array.Gella

Function Perform(ArrayName)
ArrayName.Hel=2
ArrayName.Bello="ByeBye"
ArrayName.Gella=6
EndFunction ?

This is my most important question, for it alone could de-HardCode chunks of my code. I am a fond user os the UDT (User Defined Type), for it allows for easy to remember coding, which is very important for someone who once forgot his own birthday ! Not only that, it allows me to mix different data types within arrays, and if this question is said to be yes, it will allow for some very easy coding

And Finally,
Question 4: Can I keep Subroutines within seperate files? I know that Functions can be kept in seperate files can be kept there, but I can't remember(not surprisingingly) if subroutines can be kept there. Also, are there any plans to make the seperate files more C++ like, such as being able to keep actual pieces of code in seperate files?

Thank you all for (hopefully) answering my questions.

Regards,
Ryan Wilson
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Aug 2003 23:50 Edited at: 24th Aug 2003 23:51
1: No, not yet. Most languages don't allow you to do this directly ... VB does it by allowing you to return a UDT. That's still to be added to DBPro.

2: No, and maybe never (except maybe pass it by reference instead of copy).

3: You can pass a *copy* of a UDT into a function and modify it. If your question is would it affect the original, the answer is no. Hopefully, Lee will add this in the future.

4: Yes you can, but there are limitations/difficulties with initialising variables and arrays that are held in included files. These can be overcome with a bit of creative coding.
Mentor
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 24th Aug 2003 23:55 Edited at: 25th Aug 2003 00:01
that should read "they can accept multiple parameters <comma> and return values" (in the singular sense that several functions could return several values...1 each), I would cheat and use an array (or you could return a type maybe <jk> ) since arrays are global and can carry values in and out of functions, I have a whole world creation routine that passes the world details to from one to another via arrays (that covers 1,2) 3=no, 4=dunno!, cheers.

Mentor.
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 25th Aug 2003 00:05
Write your function as a Sub instead, use global variables. It's quick and dirty but it'll save ya a headache, but don't worry you'll have plenty of headaches in other aspects of dbp.

(I can't believe I said that because as a professional vb6/vb.NET programmer I use "globals" as rarely as possible but unfortunately DBP is seriously lacking in regards of SCOPE and FUNCTIONS - but I still like it )

-RUST-
Andy Igoe
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: United Kingdom
Posted: 25th Aug 2003 00:11
OMG don't use sub functions. Dirty code and local variable interference is the devil in disguise!

You can't return multiple variables, but you can globalise variables which is faster anyway (as no variable index needs creating), when writting games it is [poor practice] but microscopically faster to use globals, and it gets around the problem of returning them.

global attack as integer

You have to do the 'as type' else the variable doesn't globalise, a quirk of DB. Anyway, that sort out that mess.

Passing in arrays, i'm not sure there was some change regarding user defined types but I dont think you can pass whole arrays in that manner. However arrays are automatically global the moment you dim() them, which should make things a bit easier for you.

Pneumatic Dryll
Rwilson
23
Years of Service
User Offline
Joined: 19th May 2003
Location: United States
Posted: 25th Aug 2003 00:54
Just to make sure I understand everything...
Question 1: This is not possible "Officially" untill a function can return a UDT, but "Technially", one can bypass this limtation by proclaiming the variables Global. Just to make sure, is this how it works?

Global Attack AS INTEGER
Global MagAttack AS INTEGER
Global Speed AS INTEGER

Function StatMath(Str,Dex,Endur,MagPow)
Str+Endur=Attack
MagPow+Str=MagAttack
Dex+Endur=Speed
EndFunction

If this indeed works, by doing so would I be able to print Attack outside of the function and have it return the right number? And since Arrays are already global, Could I do this?

Dim Nameless(1)

Function Named
Nameless(1)=2
EndFunction

print str$(Nameless(1))

and if that's possible, would the same work for a UDT Array?

Question 2: Didn't really matter in the first place, more of a question of curiousity

Question 3: Seeing as how Question 1's will be made doable by almost hte exact same thing this question was asking, which was essentially the returning of UDT's, this was quasi-answered by question 1 as well. Basiclly, in the end, we can modify the UDT's numbers within the function, but we cannot modfiy the original UDT with the function.

Question 4: Again, no biggie. In the end, it would be hazardous for me to keep my code in to many places, what with that memeory problem (The one in my head, not in the pc ). Although, I am curious as to what kind of creative programming you are refering to,IanM.

Thank you all for the info. I'll be sure to put it to good use!

Regards,
Ryan Wilson

Login to post a reply

Server time is: 2026-07-24 00:18:18
Your offset time is: 2026-07-24 00:18:18