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 / [SOLVED] Need a Simple Function Example

Author
Message
Derek Darkly
7
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 16th Aug 2016 03:46

I am having trouble coming up with an example to teach someone about functions.
(Funny that I would try to teach something that I barely grasp myself. )

The example in DBPro help is rather useless.
I want to change a couple of variables outside the function without actually typing them inside the function itself (without the GLOBAL command)

For example, I wanted to use a function to assign a random amount of money then an emotion based on the amount.
I know functions use their own set of variables, but how do I pass the outside ones into the function?

I should know how to do this by now but I can't find any coded evidence that I've ever written a good function.
This is the example is was trying to use, but it doesn't work.


Once upon a time I thought I understood functions, but I am WAYYYYY behind the curve.


Send your parents to noisy sprite demo hell... enter the D-Zone

The author of this post has marked a post as an answer.

Go to answer

LBFN
12
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 16th Aug 2016 04:49
This is a way you could do it. It seems to work okay.



What's the prohibition against using GLOBAL variables? Makes using functions a lot easier.



So many games to code.....so little time.
Derek Darkly
7
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 16th Aug 2016 12:39

Thanks for the example!
I'm not necessarily against using GLOBAL, I just thought there was another way around it.

So I guess it comes down to using arrays or GLOBAL... or just using a subroutine instead.
I'm totally fine with that.
Send your parents to noisy sprite demo hell... enter the D-Zone
Van B
Moderator
16
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 16th Aug 2016 13:53
I often use globals to return additional variables from functions. For example, if you have a function to check a memblock image point and return the colour. You could pass the colour back as a 32-bit dword, but I would also set 4 globals to the R G B and A components, like RET_R, RET_G, RET_B, RET_A - because there's every chance that you'd be more interested in the individual elements than the 32-bit dword. I'd even consider making a palette array to hold colours, then store it in the array, because you might end up wanting to mix or interpolate colours, and doing that with a little array is neater than individual variables.

My point is that globals are your friend, they let you reinforce functions when you need to and store the results for later calculations. I would always have some globalised return variables (RET_X, Y, Z, W, R, G, B, A) and a temporary array for floats. Of course you can just add whatever local arrays you need, but I much prefer to use pre-dimensioned arrays, I just don't like the idea of creating and deleting an array in a function in DBPro... I do it all the time in C++, but it would make me worried in DBPro, I'd probably blame it for the first crash I get!
The code is dark and full of errors
Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 16th Aug 2016 15:44 Edited at: 16th Aug 2016 15:46
This post has been marked by the post author as the answer.
The issue with your initial example function is that you pass variables in, but immediately write over them (meaning you don't need to pass them in, further, you don't return anything from the function so thier values are lost when the function ends.

Try this




Of course this is s pretty simplistic example and doesn't do much to explain why you want to use functions rather than just write the actions of the function out in the main body of code.

You want to use a function when you want to take a frequently repeated series of actions and compress it into a short reusable command.

I do use globals to return complex udt data when needed, and keep most of my data in udt arrays (arrays are global automatically) but you generally don't want to make a bunch of global variables just so that you can set thier value in a function, you ate basically just making a subroutine at that point, and in both cases, you can run into issues where a variable gets changed without you realizing it.


A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Derek Darkly
7
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 17th Aug 2016 02:58 Edited at: 17th Aug 2016 04:21
[EDIT]

Alright, thanks for the responses everyone... I think I'm getting it now!
Ortu, your example was what I was hoping for - to put data into a variable using a function without using arrays or GLOBAL.

One of the main things I was missing was putting my target variable at the end of ENDFUNCTION..
Here is my slightly tweaked version to show that I'm grasping it:

Send your parents to noisy sprite demo hell... enter the D-Zone
Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 17th Aug 2016 16:23
Looks good


A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Mage
12
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 21st Aug 2016 23:20
Quote: "Van B: I often use globals to return additional variables from functions. "


I'll just chime in this is due to the fact that DB Pro is not an object oriented language.

Quote: "Van B: You could pass the colour back as a 32-bit dword, but I would also set 4 globals to the R G B and A components,"


I'll add that (as far as I'm aware) it's actually ARGB not RGBA in this case. Just in case someone actually attempts to do this.
Though for most cases you can ignore this detail and just use the RGB() command.
Van B probably knows this, just adding some advice.
Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 22nd Aug 2016 01:53
Yep, it is ARGB for dbpro, that has tripped me up in the past


A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.

Login to post a reply

Server time is: 2019-05-24 17:00:54
Your offset time is: 2019-05-24 17:00:54