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 / [DB Classic] Functions (Input & Output)

Author
Message
Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 22nd Jun 2006 03:47
I'm having some trouble with functions. I can't seem to get them to take a variable, string, etc into the function.

Also, how do I take a variable, string, etc that I've sent into the function and send it's data back into the main program?

And do Functions need to be at the bottom of the code?


Here's an example of what how I'm using it, what am I doing wrong?


Functions create user defined commands correct?
Am I completely off in my interpretation of the meaning of functions?

When I dream,
I carry a sword in one hand,
a gun in the other...
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 22nd Jun 2006 13:14
Close!

Try the line x=MakeX2 to call the function, otherwise you are doing nothing wit the returned value. Or, make x GLOBAL so that the function can see and alter it.

You are right, they do make user defined commands, but those commands can be huge. You might have menu control set up as a function etc.
Crit
18
Years of Service
User Offline
Joined: 24th May 2006
Location:
Posted: 22nd Jun 2006 17:08
Also, your function has a parameter, but you aren't passing one. try x=MakeX2(x)

Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 22nd Jun 2006 22:41 Edited at: 22nd Jun 2006 22:41
Crit: Well spotted, although if x is Global, you need not pass it or return it.

[Edit] Although thinking about that, it is a really bad habit to get into!
Crit
18
Years of Service
User Offline
Joined: 24th May 2006
Location:
Posted: 22nd Jun 2006 23:33
Do you think its a compiler error to let something like that slide? I don't think DB supports function overloading or optional paramaters, does it?

Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 23rd Jun 2006 04:15 Edited at: 23rd Jun 2006 04:19
Ok, got some questions then:

Dream And Death:
Quote: "Try the line x=MakeX2 to call the function, otherwise you are doing nothing wit the returned value. Or, make x GLOBAL so that the function can see and alter it."

Crit:
Quote: "Also, your function has a parameter, but you aren't passing one. try x=MakeX2(x)"


Ok... So then like this?


or this:


Why is the variable 'X' placed before =FunctionName .
And why is a variable in parentheses being placed after it? X=FunctionName(X)

Also, How do I make strings & variables global.

-EDIT- (The following lines have been added.)
Small code snippets examples would be appreciated.

When I dream,
I carry a sword in one hand,
a gun in the other...
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 23rd Jun 2006 14:29
You have two ways of doing it

First method - function affecting a global variable
The first is to use a global x variable (I find it better practice to to call all global variables glbl_x , or glbl_y to remind myself, but for know, we will keep to simply x.


The function doesn't need any inputs (nothing in the brackets, although they need to be there) and no output, so nothing comes after the EndFunction.

Second method


This function needs an input (in the brackets) which is an integer value and returns an output, after the EndFunction. When you call the function, you need to assign the answer of the function to something - in this case, as you want to increase X, you need to set X to the new value.

What may be confusing you in the second example is using X in the main program, and also in the function - they are in fact two different variables! One X can only be seen in the main loop, and the other can only be seen whilst you are in the function. To make it easy, we can rewrite example 2.



I hope this helps to clarify things.
Mr X
18
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 23rd Jun 2006 19:58
Just something I noticed:

You dont need the brackes when you do an output.
Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 24th Jun 2006 00:00 Edited at: 24th Jun 2006 00:07
@Dream And Death:
Thanks for sharing the glbl_ naming system, I'll try it out.

I think I understand it now, thank you!
(That last code snippet explained a lot.)

I'll type out an example of my understanding just so I know I've got it.

The 'X' variable before =MakeX2 receives the output from the function and the ('Variable') after MakeX2 is taken into the function.


@Mr X:
Thanks, I missed that in the manual.


Three questions (Hopefully three final questions. )
Is it possible to take more then one variable into the function?
Is it possible to output more then one variable from a function?
Global variables auto input/output from functions? (... I didn't phrase that well. Global function can be modified by functions without being input into them? That sounds better. )

Functions are interesting, I'm going to be able to do a lot more easily by using them. I still have so much to learn.

When I dream,
I carry a sword in one hand,
a gun in the other...
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 24th Jun 2006 00:34
In order:
Is it possible to take more then one variable into the function? Yes
e.g. function Many(First as integer, Second as float, Third$ as string)
Is it possible to output more then one variable from a function? Nope, only one. [See below]
Global variables auto input/output from functions? Global variables have global 'scope' and can be seen (and affected) by any function.


Functions are an important area of programming and you should practice with them.


[Extra note]
There could be a way around returning more than one variable.

If you have some a function like this (I'll use integers cause its easiest:



you can split Output$ at every comma and assign the values.

I'll give you a boost:
Mr X
18
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 24th Jun 2006 18:52
Dream and Death
You forgot one thing. You have to convert the integers into strings before you place them into the string.
should be
.
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 24th Jun 2006 19:53
{Tips hat to Mr X} You are right, I missed that. Too used to VB's variants!
flock
18
Years of Service
User Offline
Joined: 10th Mar 2006
Location: Earth
Posted: 24th Jun 2006 19:58
Can you do that in DB? I was wondering how you could assert real or integer vars into a text command line?

a.k.a. "flockhound"
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 24th Jun 2006 20:14
Yeah.

Use the Instr() function I put a few posts above to find out where to split the text command line (or write your own split() command, or search for one on the forum)

Then use val() to turn the strings back to integer or float values, and assign to the required variables.
flock
18
Years of Service
User Offline
Joined: 10th Mar 2006
Location: Earth
Posted: 24th Jun 2006 21:52
ic. Thanks, DnD

a.k.a. "flockhound"
Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 2nd Jul 2006 04:44 Edited at: 2nd Jul 2006 04:45
Thanks for the code snippets Dream And Death & Mr X.
(Very helpful)

*Codes... stops in confusion...*

How do I declare a variable as global?

I thought it was:
GLOBAL Name
But that didn't work.

Is it:
DIM Name(1)
Isn't that just a 1D array?

I could have swore I saw the command listed as "Global VariableName" somewhere.

When I dream,
I carry a sword in one hand,
a gun in the other...
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 2nd Jul 2006 11:13
Er, yeah, check my 12:29 post on the 23rd. It has the following snippet:

That shows you how to do a global variable.

Also possible to do
global x as integer
global myname as string

I do find naming conventions important, as I mentioned earlier.

Its best to develop your own, and mine change slightly from project to project, but I would tend to:
global glbl_int_x as integer
global glbl_str_myname as string

Then by definition when I am using the variable, I know its scope and its datatype. for local variables I tend to either neglect the start of the indicator or use lcl or tmp:
dim lcl_int_count as integer
dim tmp_flt_sum as float
dim str_input as string

You are right by the way,
dim x(1) does dim a 1D array of size 2 called x. This by definition makes x(0) and x(1) global as arrays are by definition global in scope.

Hope this helps!

"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library

First you Dream, then you ... - Neil Gaiman, 2001
Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 3rd Jul 2006 01:06
@Dream And Death:
DBC's error message:
Quote: "global x
"Syntax Error, Unknown Command""

Your global code snippet is producing this error when I run it.

When I dream,
I carry a sword in one hand,
a gun in the other...
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 3rd Jul 2006 10:50
{Grimaces}

Big apologies Swordsman - there is no global command in DBC - I'm getting too used to DBP.

Arrays are global, but the DBC version of a 'global' variable is one defined outside of a function, and then can't be seen inside of a function. You'll have to pass the variable into the function.



"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library

First you Dream, then you ... - Neil Gaiman, 2001
Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 3rd Jul 2006 11:13
@Dream And Death:
Thanks for the code snippet.

I'll use 'DIM Array(0)' as a global variable substitute,
or can arrays not be seen by a function if they are declared outside it?

Easy way to check...
*Loads the DBC Editor and quickly builds a test program...*


Excellent, this will work.

When I dream,
I carry a sword in one hand,
a gun in the other...
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 3rd Jul 2006 12:20
Good work round!

"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library

First you Dream, then you ... - Neil Gaiman, 2001

Login to post a reply

Server time is: 2024-09-25 03:39:59
Your offset time is: 2024-09-25 03:39:59