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 / Return not working in a function??

Author
Message
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 15th Nov 2011 13:43
I have been building my program in two separate pieces, now I want to include one source file as a function in the main program but it complains about not being able to use the RETURN command in a function?? I use a lot of sub routines in my included source file, is that not possible at all?

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 15th Nov 2011 13:53 Edited at: 15th Nov 2011 13:56
To leave a function prematurely use the EXIT FUNCTION command




revenant chaos
DBPro Master
19
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 15th Nov 2011 14:09 Edited at: 15th Nov 2011 14:10
@Kevin Picone: Been away from DBPro for a while I see, that isn't the proper syntax.

zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 15th Nov 2011 17:23 Edited at: 15th Nov 2011 17:38
Quote: " I use a lot of sub routines in my included source file, is that not possible at all?"


The include files must be comprised of only functions. Sub-routines will not work. Though... I have never attempted coding a sub-routine inside of a function.

There are other caveats with regard to included files in the help


Quote: "
#INCLUDE

You can use this command to include the function declarations of other Dark Basic Professional programs.

[SYNTAX]
DBA Filename

[Parameters]
Value
Enter the appropriate value here.

[Returns]
This command does not return a value.

[Description]
You can use this command to simplify your main program by writing sub-programs that hold re-usable independent functions.

The #include will associate all the functions within the sub-program with the main program during compilation.

The Filename String must specify a Dark Basic Professional program in the current project directory. You can #include up to 255 sub-programs in total. Sub-programs can also use the #include command to further nest sub-programs, but all sub-programs compile into the main program so duplicated function names are not allowed.

Be aware that included source code is appended to the end of the program, which means such declarations as TYPE and FUNCTION names included are only valid for use within the included file. If you want to use your TYPE globally, ensure it is declared in your main program.
"


Your signature has been erased by a mod please reduce it to 600 x 120.
Sixty Squares
20
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 15th Nov 2011 18:47
Quote: " Sub-routines will not work."


Contrary to what the help file says I've done this several times... I'm pretty sure sub-routines can be created within include files and referred to in the main source file. Subroutines are also useful for declaring TYPES, which cannot be declared within functions.


Guns, cinematics, stealth, items and more!
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 15th Nov 2011 19:06
Quote: "Contrary to what the help file says I've done this several times... I'm pretty sure sub-routines can be created within include files and referred to in the main source file. Subroutines are also useful for declaring TYPES, which cannot be declared within functions"



There's a good chance I am wrong on that one. I thought I remembered reading that somewhere along my journey. With dbpro projects,, is there really a need to even bother with #include???

Your signature has been erased by a mod please reduce it to 600 x 120.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 15th Nov 2011 20:43
Quote: "With dbpro projects,, is there really a need to even bother with #include???"


Is there an alternative?

Right now, I am simply gonna replace all GOSUB-routines with functions I might get a cleaner code even

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Nov 2011 21:31
Any code can be placed within an included file - it is simply appended to the main file when you compile.

Depending on the placement of that code though, it may not get executed (in the same way that code between two functions and with no label cannot be executed - it's just not accessible).

Constants and types do not need to be executed at all - wrapping a gosub around them is a waste of time and effort.

Remember also DBPro's two-phase approach to variables: globals and arrays can be declared within a dead-code section, but unless you actually run that code they won't be initialised (unless you arrange to initialise them elsewhere).

Sixty Squares
20
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 16th Nov 2011 04:09
Quote: "Constants and types do not need to be executed at all - wrapping a gosub around them is a waste of time and effort."

Very true. I accidentally blurred the actions of declaring types themselves and declaring variables AS types into one. Thanks for clarifying that, my apologies


Guns, cinematics, stealth, items and more!
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 16th Nov 2011 13:44 Edited at: 16th Nov 2011 14:39
Quote: "Is there an alternative?"


Yes,, of course. Would I have suggested one otherwise?


.................................................................................................

Perhaps I should explain what I meant by "DBpro Projects"

.................................................................................................


[Step 1]

Launch the default DBpro editor.
[Step 2]

Create a New 'empty' Project named "NoIncludeTest".
(The default file should be named "NoIncludeTest.dba" automatically.)

[Step 3]

Go to 'Solution Explorer' and add a new file to the project by clicking on the [File] icon and name the new file "File2.dba".
(If you already had a "*.dba" file,, you could click on the 'Folder' icon to import an existing file.)

[Step 4]

Copy & Paste the following source code into the projects default file.
NoIncludeTest.dba


[Step 5]

Copy & Paste the following source code into the "File2.dba" file.
(File2.dba is in a seperate tab in the editor by default. If not choose "File2.dba" via Solution Explorer.)
File2.dba


[Step 6]

Compile & Run.

.............................................
Output of the test project
............................................


Hello World! Include directive isn't necessary!!!!


Your signature has been erased by a mod please reduce it to 600 x 120.
Stab in the Dark software
Valued Member
23
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 16th Nov 2011 14:48
Cybermind,

As stated above you can declare a UDT any where outside a function, just make sure when declaring a UDT of a UDT that you declare them in order. Example below.




[img][/img]


WindowsXP SP3,Vista,Windows 7 SP1, DBpro v7.7RC7
Stab In The Dark Editor
The coffee is lovely dark and deep,and I have code to write before I sleep.
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 16th Nov 2011 19:40
Quote: "it complains about not being able to use the RETURN command in a function?"

I cringe at the thought of trying to return from a function! Talk about a buffer overflow waiting to happen!

My signature is NOT a moderator plaything! Stop changing it!
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 16th Nov 2011 22:33
Sounds like a Visual Basic confusion, since the return command is used to return function results in VB.

Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 18th Nov 2011 22:04
Hey Kevin,

Won't your code cause a stack problem?



If you call this function enough times (probably hundreds of thousands of times will be necessary) won't you have a problem with the IF branch never being closed? If we never hit an ENDIF won't the stack grow and grow and grow?

This might be autoresolved (perhaps DBP automatically discards any open IF branches in a function that it's ended), I've never tested it. Still, it goes against the grain to allow an IF to go unENDIF'ed. I'd rather REPEAT UNTIL A > 1000 and allow the function to end naturally.

Daniel wright 2311
User Banned
Posted: 18th Nov 2011 23:39
why not just put the function in your main program?

there is no need to split them up unless you are using one program to launch a diffrent program all together.

this also can be done with just one program.

my signature keeps being erased by a mod So this is my new signature.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 19th Nov 2011 00:10
Quote: "why not just put the function in your main program?"


I have about 3000 to 4000 lines, it gets quite cumbersome to scrool through one document with between 3000 to 4000 lines of code, and there will be more code

Right now I am correcting errors in one part, when done I will change all GOSUB's to functions and merge some codes

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Daniel wright 2311
User Banned
Posted: 19th Nov 2011 00:17
I see your point,

Just indent your commands then if you need to find anything search for it with the rem indent you choose for that part of code.

like this

rem here is my example a function

then when i need to find it search this way

my example a function

my signature keeps being erased by a mod So this is my new signature.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 20th Nov 2011 22:41 Edited at: 20th Nov 2011 22:41
I scroll the code up and down ferociously at the moment, as I am hunting a bug that appears every second time you press a chess piece in certain situations, making it real hard to locate. I have hunted this bug for days now, and while I have not found the bug I have sorted out many other things and trimmed the code while hunting this annoying bug Searching is too slow for me right now

Anyway, I hope for a WIP soon

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 26th Nov 2011 16:38
I have been hunting a bug in my chess program for more than a week, today I found it Four lines had to be moved four lines up and one parameter changed, wuut wuut! During my bug hunt i optimized a lot of other code, so it was not wasted work time though

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...

Login to post a reply

Server time is: 2026-07-10 03:59:06
Your offset time is: 2026-07-10 03:59:06