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 and local and global variables

Author
Message
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 15th Jun 2003 19:34
I've just bought DB Pro.

I have to say I am a bit non-plussed by what I can only describe as the minimal amount of written material that accompanied it. The package is marketed for the absolute beginner but I have to say I think the material might as well be written in Chinese for all the use it would be to an absolute beginner.

Anyway, that complaint aside, I note with some surprise that none of the help (whether hard copy or on-line) gives any explanation of how DB Pro handles global and local variables. Are we to assume its exactly the same as all other variants of Basic?

Also, the section on functions and their interaction with variables is opaque. In a programme I am writing I was planning to have a function which returned a 1 value into a variable for each of the keys that might be pressed. This would have worked roughly as follows:



As I have a C programme written using the same system and this would have made it easier to port to Dark Basic. However, I discover that this apparently does not work.

Oh well.
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 15th Jun 2003 19:35
The accompanying and on-line help material doesn't explain #CONSTANT either.

Moggie100
22
Years of Service
User Offline
Joined: 15th Oct 2002
Location: Behind You...
Posted: 15th Jun 2003 20:55
okies:

Global Variables:
Global Variables are where data can be read and written at any point in the program - in the main loop, or in functions, or anywhere else.

Local Varialbes:
Local Variables are data that can only be accessed in its particular part of the program, for instance, if you have a local variable in the main loop, you cannot read it in a function without making it an input parameter of the function. You can however read and write to the variable in subroutines (goto & gosub commands) because they are essentially part of the main program and are not seperated in any way except for location.
In functions, local variables can only be accessed in that function, and will not affect any variable in the main program, even if there is an identicly named variable to one in the program outside the function. however functions can pass out 1 variable into the main program, and can take variables from the main program in the form of parameters. eg:

do
inc outside#
outside#=test(outside#)
loop

function test(inside#)
inside#=inside#+0.2
endfunction inside#

In this code the function takes in the variable 'outside#' which is local to the main loop, and uses its value to set an internal variable called 'inside#' within the function.
Then after incrementing the 'inside#' variable by 0.2 the function then outputs the 'inside#' variable into the main loop where it is taken by the 'outside#' variable.

Hope that helps

Ask if u want anything explained further.

Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 16th Jun 2003 19:05
Thanks.

Thats roughly what I thought. I shall have to use subroutines. Thats a bit disappointing but not to worry.

On the whole I am, however, pleased with DB Pro. Its written material is sparse but the programming language appears powerful.

Moggie100
22
Years of Service
User Offline
Joined: 15th Oct 2002
Location: Behind You...
Posted: 17th Jun 2003 02:23
if u're trying to get values taht you can change irrespective of where-ever they're being read from you could use a 1 box array as a variable for example:

dim life(0)

would create an array with only one value - then to change it anywhere in the program all you'll have to do is:

life(0)=1043287

or whatever, its a bit of a waste of an array, but I havent found a limit on arrays anywhere yet But that can be read from anywhere, functions included.

Bloodshot
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location: United Kingdom
Posted: 18th Jun 2003 01:39 Edited at: 18th Jun 2003 02:05
Just bought mine too, Philip!
DBPro is something to finally fill that AMOSPro shaped void in my life since moving over from the Amiga to the PC all those years ago.

There's nothing for PrintC either, (though the command is listed in the index!???)


IMPORTANT: If you suddenly decide you want to declare any of your variables as Global, make sure you do it before you assign any values to those variables in your program, because the variables contents are reset to zero by the Global command!

Example:
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 19th Jun 2003 19:39
This is probably a dense question, but if your program alters the value of a global variation in a function, does that altered value survive the function if it is not formally returned by the function by:

ENDFUNCTION variable

I assume that the altered value does survive, would be grateful for views on this point.

Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 19th Jun 2003 19:40
I've just noticed that "Global" is not mentioned in the DarkBasic Pro manual and accompanying materials either.

This is very odd. Why is it that the manual is not comprehensive?

the_winch
21
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 19th Jun 2003 20:39
The manual has some features that are not documented or incompletely documented. I am sure dbs has their reasons, perhaps limited resources better directed elsewhere.

Yes the global variable will survive changes if it is not returned by the function. You should try these things out yourself. It would only have taken a minute to make a sample program to test what happens.

Login to post a reply

Server time is: 2024-11-24 18:26:15
Your offset time is: 2024-11-24 18:26:15