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 / Function Parameter and Output Help

Author
Message
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 2nd Jun 2012 18:52
Okay, I have a couple different questions on how to use function parameters.

(1) I want to develop a single function to average 2-5 numbers really quickly, but obviously the function would behave differently depending on how many numbers were averaged in. Is there an easy way to do it? I might have 0's in the averaged numbers, so I can't just input 0's where I don't want parameters.

(2) I want a function that removes everything after a set of parentheses, and another that takes everything before it. I also want it to ignore spaces.

For example, reading:

$string = Intelligence (20%)

Would return two different things if put through the functions

line_traitname($string) would return "Intelligence"
line_traitvalue($string) would return "20%"

(3) Related to 2, how do I reliably turn a percentage in the form of a string into a decimal for calculation? Not all traitvalue returns will be percentages, some will remain strings, some will be other types of values. Can someone link me to a page explaining the types of values and what numbers they can store?

(4) Also, I'm really confused as to how RGB values behave when input into functions, and I imagine what could easily be three to four hours of work being blown on not understanding what would otherwise be really easy, if I could get a little forewarning.

Basically, I have several functions in mind to fix or change colors. I want to have them in 3 seperate variables for processing, but from what I understand, functions only return one value.

So, like
FUNCTION color_redshift(R,G,B,shift,lower)

R= R+shift
G= G-lower
B= B-lower

ENDFUNCTION

(That's just pseudo-code to show you what I mean)

I can't make it return three separate values, right? I sort of need it to, as the variables are all stored separately. Unless I could get a traitvalue string (explained in 2) to be treated as an RGB value. Which I highly doubt that'll even work.
zeroSlave
15
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 3rd Jun 2012 02:34 Edited at: 3rd Jun 2012 03:12
(1) My suggestion would be to use array lists with a couple functions. There may be a better way to do this, but this is the first thing that came to mind. Here is some code to show an example of it in use:


(2) You should check out the left$(), right$(), len(), trim$, & mid$() functions. Here is some code:


(3) I'm not sure about what you mean. If you need to turn a string into a number, you can use the val() function (used in the above code) to return a number instead of a string. You can also use the str$() function to return a string from a number.

(4) For the color part, you can send a dword to the function and return a dword from it for the color. i = rgb(100,56,34) would return a dword. Then you can use the rgbr(), rgbg(), & rgbb() functions to return the integer value of the respective color.
i = rgb(100,56,34)
rgbr(i) would return 100
rgbg(i) would return 56
rgbb(i) would return 34

Here is code to show it working:


Hope this helps and isn't to confusing.

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 3rd Jun 2012 10:54 Edited at: 3rd Jun 2012 10:56
Quote: "(1) I want to develop a single function to average 2-5 numbers really quickly,"

This is a much quicker (easier, coding-wise method) but if you want more flexibility, stick with zeroSlave's example. It depends largely on the application.


Quote: "(2) I want a function that removes everything after a set of parentheses, and another that takes everything before it. I also want it to ignore spaces."

Here's a basic example. It's not the best but you can customize it. Have a look at the string-manipulation function in the DBP help. If used correctly, you only ever need the LEFT$, RIGHT$ and LEN functions. However, the others like MID$ and FIND FIRST TOKEN$ make life a lot easier.


Quote: "Related to 2, how do I reliably turn a percentage in the form of a string into a decimal for calculation?"

What zeroSlave said

Quote: "Can someone link me to a page explaining the types of values and what numbers they can store?
"

Open a blank file in the editor, press F1. Click "Principles" then "Datatypes and variables". All you basically need to know.

Quote: "Also, I'm really confused as to how RGB values behave when input into functions,"

The other thing zeroSlave said.

Quote: "Hope this helps and isn't to confusing."

It shouldn't be. In fact you could be writing tutorials. That was very well put and a lot more coherent then many of the "|-|0\/\/ |-0 c0|)3 ||\| |-3|-| Darkbasicz"* floating about the web.

Translation:


Login to post a reply

Server time is: 2024-11-21 23:36:15
Your offset time is: 2024-11-21 23:36:15