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 / I am having problems, help please

Author
Message
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 7th Apr 2013 05:32
This is the code



If I run this and press 1 to start the screen for the adventurers guild comes up, not a problem. That's what I want. If I hit 1 on that screen I get an error that says I have hit a function in mid program. What am I missing here?

Any help would be great!!!
Thanks in advance!
Maxxim
12
Years of Service
User Offline
Joined: 1st Apr 2013
Location:
Posted: 7th Apr 2013 06:01
Hi! When your program exits from CreateCharacter function it sees a declaration of EndGame function. Functions should be declared outside of main program loop, that's why it's throwing an error. You can have several ways of solving it:

1.Prevent program from exiting the AdventurersGuildMenu function. Add some input after



2.Create a main DO...LOOP loop, for you functions to have a point to return.

Also I don't really see why you need to use functions here, as you don't pass or return any values. Instead of it I would use subroutines (GOSUB).
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 7th Apr 2013 06:19
I got this code to work. I set up a do loop and played around with it and got it to work!
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 7th Apr 2013 07:29 Edited at: 7th Apr 2013 07:30
Quite literally my friend, after the adventure screen there is no End statement. The program flows out of the IF-ELSE block right at the function declaration. Insert an end statement before the first function declaration to correct the problem.

Edit: Ignore this, I hit post message long after your reply.

Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 7th Apr 2013 18:21
Is there really a difference in using a gosub or a function? I have done a forum search on it and some say a function is a tad bit slower than a subroutine. Not really a problem for me now. I do see your point though, about not returning a value. I just don't see what difference it makes. I may change it up just to do it, just to try it out.

This is right out of the Hands on volume 1 book

Quote: "
Also, it is not possible to create local variables within a subroutine.When variables
of the same name are used in both the main program and a subroutine, these are
assumed be one and the same variable.

As a general rule, it is best to avoid the use of DarkBASIC Pro's subroutines and
stick with functions since they offer a much greater flexibility.
"
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 7th Apr 2013 19:29 Edited at: 7th Apr 2013 19:50
In the BASIC language, a proper subroutine is a method that does not return a value but can contain locals. In DarkBASIC this is not the case, they are limited to globals; anything declared as local is not truly exclusive to a single subroutine, it is exposed program wide.

Think of a variable as a member of staff in an organization, a line of code as a task; and a function as a department in the organization. All staff have names, but are listed under the department.

Jim Brown in department 1 is a different person to Jim Brown in department 2, 3, 4, 400, 4000 and so on. With globals and variables declared in subroutines there can never be more than one Jim Brown in the organization; unless declared as an array in which case Jim Brown is a list of Jim Browns, not just one.

All the staff carry out tasks in the department and work with the equipment supplied to the department. If they need more equipment or assistance they must call on another department; which can be seen as a function. If unavailable, they must outsource via third party; which could be seen as a plugin. Once the job has been done by the staff, they are dismissed; they've left the building. Jim Brown cannot be accessed until the function is called again.

Now a subroutine is not a department that contains its own staff, these subroutines do not have their own variables. Jim brown in subroutine is the same Jim in subroutine B; and he works 24/7; he can only go home when the program has been closed.

For that reason, globals and variables declared in sub-routines take up memory when not in use. A number of staff allocated to a job in a department only take up office space when they are working; with staff who are not local to a department, they take up office space when not doing anything; they generally cannot be dismissed; with the exception of array entries and strings cleared with free string$().

Generally, local variables are have faster easier access than globals; so technically, functions in the long run, will work faster when they make use of locals; there is no need to look for Jim because he is in the same room.

The real major difference between an organization of functions, and an organization of departments of staff is that digital functions can call on themselves; and when it does this, that function and its variables are created in a unique area in memory; Jim Brown number 2. This process is called recursion, where the function needs to do what it itself does to something inside of it. Subroutines cannot do anything to anything inside of themselves, and must never be used with recursion.

With binary, A cannot operate on A; that would be unary. When a Function A calls Function A, it is actually similar to Function A calling Function A-2. When subroutine A calls subroutine A, it is calling itself which is a loop. If function A, calls A-2, and A-2 calls A-3 and so on right down to A-9999999; you will produce an error termed a stack overflow. Inner calls to functions need to be kept under control.

So functions are more effective than subroutines. Any thing your not certain about just ask.

Mobiius
Valued Member
22
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Apr 2013 20:05
Quote: "Jim Brown in department 1 is a different person to Jim Brown in department 2, 3, 4, 400, 4000 and so on. With globals and variables declared in subroutines there can never be more than one Jim Brown in the organization;"

That analogy is incorrect. Since you state the departments are functions, there can in fact be multiple Jim Browns. Variables in subroutines are NOT global. They only exist in the same scope of the area in which they reside. For example, see my below code..



This code would display the following:

69
69
96
0
96

This is because Variable is a local variable. Functions cannot access local variables declared outside of the function. Imagine DBP was like C++, the code you type at the 'top' of the editor is in the function void main() think of it as being in it's own function, just without the definition. Subroutines are ways of jumping the program flow whilst still accessing the same variables.

Functions are the same, you can even have subroutines in functions, which will only access the variables created in that function.

I live for video games! (And beers, and football, and cars!)
See what I live for here: [url]http:\\www.TeamDefiant.co.uk[/url]
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 7th Apr 2013 20:56 Edited at: 7th Apr 2013 21:12
Take a closer look

Quote: "Quote: "Jim Brown in department 1 is a different person to Jim Brown in department 2, 3, 4, 400, 4000 and so on. With globals and variables declared in subroutines there can never be more than one Jim Brown in the organization;"
That analogy is incorrect. Since you state the departments are functions, there can in fact be multiple Jim Browns. Variables in subroutines are NOT global."


Indeed variables in sub routines are not global; that's not quite what was said:
Quote: "With globals and variables declared in subroutines there can never be more than one Jim Brown in the organization;"


It said 'globals and variables in sub-routines'; not saying that 'variables in sub-routines are global'; not the same. You have misinterpreted where I am going with my point; which is global A, and local A in a sub-routine should not co-exist; unlike with functions:



Note that the function local did not alter the global

Quote: "Since you state the departments are functions, there can in fact be multiple Jim Browns"


I did not dispute that, you are actually agreeing with what I said here:

Quote: "Jim Brown in department 1 is a different person to Jim Brown in department 2"


There can be multiple Jim Browns, but beneaf the surface, they reside in different areas of memory and are thus not the same Jim Brown, the same way a limb named 'Hand' in one character is not the same 'Hand' in a cloned character (Which is not technically the case in an instanced one)

Quote: "Functions are the same, you can even have subroutines in functions, which will only access the variables created in that function."


There are three things wrong with your point there;

One; you can't compile a return statement in a function; therefore the label will work as a GoTo link; it wouldn't be classed as a sub routine which requires a return statement. If this was Visual Basic, it wouldn't work because you simply can't declare methods in methods without utilizing a lamda expression or reflection.

Two; if it would have worked, it would be quite dangerous as the following code demonstrates; you could call a sub-routine from one function that resides in side another:


Which brings us to number three; sub-routines are not local, they are global; if they were local the code above would not work; it would mean MyLabel: is a local reference exclusive to function B; which cannot be true because function A can access the label declared in function B; this is consistent with the fact that globals can be declared in functions.

Login to post a reply

Server time is: 2025-05-17 16:49:04
Your offset time is: 2025-05-17 16:49:04