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 / Another simple function question

Author
Message
Chernobyl
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 9th Jul 2007 23:31
Just trying to understand the logic here. Doesn't a program load a array into memory regardless of where the array is in the program sequence?

why does this work:


BUT this does not:


Tom J
19
Years of Service
User Offline
Joined: 4th Aug 2005
Location: Essex, England
Posted: 9th Jul 2007 23:44 Edited at: 9th Jul 2007 23:47
The bottom one doesn't work due to the commands being in the wrong order, in the bottom one you are trying to print the array before you have created it with the dim command.

The top one works because you use dim before you use the function that requires the array.

The order of the code can be very important

Chernobyl
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 9th Jul 2007 23:54
Oh thanks, simple enough, for some reason I thought arrays were loaded first regardless of their position, which now that I look back on it makes not too much sense.

Should arrays always / usually come at the beginning of a program, before functions, commands etc ?

WHY doesn't this apply to functions? Why does it let me call a function before I create the function?

Xolatron
18
Years of Service
User Offline
Joined: 12th Mar 2006
Location: The Star Forge Language: DBpro
Posted: 10th Jul 2007 01:05
Yes, global arrays should usually be declared at the beginning of a program if you plan to use them throughout that program.

Arrays and variables are quite different from functions:
- Arrays are created on use of the 'dim' command, and can be deleted using 'undim'
- Functions always exist as part of your program, like subroutines or loops. Functions are never created. They, like subroutines, must come *after* the 'end' statement in your program
- Arrays are stored in run-time program memory
- Functions are stored as part of the exe file (which is copied into ram on runtime, but *always* exists while the program is running, unlike the memory used to store arrays)

Basically, functions are custom commands stored in the program itself, while arrays are run-time variables created as the program executes.

And just a note on the array: Technically it should be
'global dim a$(5)' if you want to retrieve its contents during a function. Each function has its own variables (locals), and normally can't access regular, non-function variables unless they are declared as globals ('global myInt as integer : myInt = 3'). The reason your program works their is because DBpro apparently assumes that all arrays are global (while it assumes integers, strings, bytes, etc. are local). It would be better to always declare your global variables as global.

-Xol

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Jul 2007 01:30
Quote: "must come *after* the 'end' statement in your program"

Functions can go anywhere. It just depends how you work them.

A standard DBPro program structure might look like this:



However, you can infact do it like this (which is what I prefer):


Daggeth
17
Years of Service
User Offline
Joined: 3rd May 2007
Location:
Posted: 10th Jul 2007 12:54
DBPro, because it is a 'BASIC' language handles alot for you, this includes initializing all functions when the program starts.

If you program in C++, you cannot call a function before you explicitly define it (or tell the compiler that you have an intention to define it )
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Jul 2007 17:35 Edited at: 10th Jul 2007 17:37
Quote: "They can't!"


For examples sake, I will use my game's main source file, which I will tell you now, does compile, and does work.



As you can see, there is no end statement. All it is, is functions, types, a few constants and a few includes. The main loop is inside the main() function.

Quote: ""start()" is your main code, and you cannot move it down"

It will get an error if it runs into a function declaration, but that has nothing to do with the actual structure of the code.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Jul 2007 18:29
DBPro, unlike languages such as C++, do not rely on functions to run on, and that is why, say, DBPro doesn't automatically call main(). DBPro is an imperative language, and performs actions in the order they are given (unless a function or sub is called), and that is why putting a function at the begining will give a compiler error, and that is why start() has to be called. That has nothing to do with the actual structure of the language though. You are saying that is must first be all the stuff you create, etc, main loop, end statement, then functions. That's simply not the case.

As I said, functions can go anywhere. Just one has to be called.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Jul 2007 18:46
Quote: "the code that will be executed first"

The functions arent executed after. I understand what you are trying to say about the end statement, but you will find it's not necessary (hence calling the start() function).

I have to admit, it would be cooler if DBP had a start() or main() function that is called automatically. That would keep people's code alot tidier, but unfortunatelly, it can't That's why work arounds have to be used, and adding an extra "start()" somewhere isn't really a problem

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Jul 2007 22:57
That's just a matter of taste, heh.

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Jul 2007 23:18
Quote: "That's just a matter of taste, heh."


Yes it is.

Personally, I think your second 'function-based' example is using functions exclusively for the sake of it and is very 'non-standard' for a way of writing a program in BASIC.

If I wanted to write code like that I'd fire up C++.

I know you prefer using that method, but I can't see any benefits of doing so and your first 'standard' method example is a lot clearer.

Functions and procedures by default go at the end of your code only because the main program loop is normally not placed in a function and is at the start of your program. You could have the main code at the end and use a Goto at the start to jump over the functions and procedures - but who in their right mind would want to do that?

But, as you say - whatever floats your boat!

TDK_Man

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Jul 2007 23:29
Heheh, well, I would fire up C++ too, unfortunatelly there are no engines quite as easy as DBPro (not that I know of anyway). It just makes my program feel alot more organised and less clutered.

Login to post a reply

Server time is: 2024-11-25 15:44:44
Your offset time is: 2024-11-25 15:44:44