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.

Author
Message
haliop_New
User Banned
Posted: 2nd Dec 2014 08:26
why should i use that rather then a simple function ?
Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 2nd Dec 2014 08:40
<opinion>You shouldn't.</opinion>

Seriously I know of no good reason to use gosubs over functions. I think they're just a legacy from earlier BASIC dialects. I'd be fascinated to see if anyone else in the community can think of a reason though.

paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 2nd Dec 2014 08:57
There is possibly a tiny efficiency gain over using function ; the default function may set up a stack frame with zero parameters and gosub doesn't.

I agree though ; it's a hangover.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd Dec 2014 09:41
gosub is a part of freedom, sometimes u need to jump around,
in other basic u can use goto & gosub also inside a function.
u have access to the same vars at same level.

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Dec 2014 09:44
It's a matter of preference. My preference is functions.

With functions you can create an include file of encapsulated functionality. For example, I have a library of effects (animated slides, scaling, rotation, berp/lerp etc movements) and I just drop it into any new project.

Quidquid latine dictum sit, altum sonatur
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 2nd Dec 2014 11:14
I use Gosubs to jump from my Main source code, to the Initialisation section of any included sources (The bit where I declare types and stuff).

For example...

main.agc


system-module.agc


It may not be the best way of doing it, but it works for me. You can't (Well couldn't anyway) declare types in a function (or again, I may be thinking of DBP).

CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 2nd Dec 2014 13:11
I try to avoid gosub where possible after a recent issue with my Glow Biker game I'm working on. Mixing functions and gosubs can lead to stack corruption (I think). First run on Android worked fine, subsequent attempts would crash (phone would freeze for a few seconds then drop back to home screen). Replaced all my gosubs with functions and it's all working good again.
You're probably okay using one or the other, but I think both together can cause problems.

Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Dec 2014 13:27
I still use them. I don't think we can consider them in the same way as we do with other languages. I mean, one thing I don't like about AppGameKit is it's 'meh' approach to array handling - I hate that I can create an array in a function that is not restricted to that function, this can cause real issues. We can declare an array globally or locally, but all arrays are treated as global, so it's redundant.

What if I want to create and use a temporary array, I have to delete it at the end of the function?, that's horrible! - I'd much rather that the array had to be declared as global inside a function, otherwise it's gone once the function has completed.

So I tend to always use an initiate subroutine and define the globals and arrays in that, rather than doing any array definition in a function. I often use an 'update_enemies' subroutine as well for example, called from the main loop. If something is being called every loop, I'm not sure that a function is the most efficient way - a gosub can jump straight to the relevant part of the program - a function call needs more consideration and is probably a little bit slower. I find it's clearer to just gosub from the main loop with a lot of things.

I am the one who knocks...
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 2nd Dec 2014 13:47
I disagree about some of this. Globals should never be declared in a function. If possible, there shouldn't be globals at all!

The whole point of encapsulation is that things should be destroyed when they go out of scope. The central problem with AppGameKit Basic's model is that it's almost impossible to destroy anything apart from transient things like sprites and sounds.

-- Jim - When is there going to be a release?
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 2nd Dec 2014 13:49
I use Gosub only for going to an included file and define variables.

If the #insert function will work in the next version, I can use that instead.

#insert inserts the extra file at the next line and executes it at once, #include appends it to the main file, and I need to go "down there" to run the code.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Dec 2014 14:03
Yeah, that #insert command will be handy - can have the whole game array, global and type definitions in a single file, nice and easy to check on variable names with that too.

I am the one who knocks...
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 2nd Dec 2014 14:11
Would it not be better to have some some concept of "name space"?

-- Jim - When is there going to be a release?
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 2nd Dec 2014 14:22
You can pretty much do namespace using identifiers. If you have (say) a drag and drop library if you prefix everything with ddrop_ then it is effectively namespaced and its easy to change it if you absolutely have to. Used to do this when I developed in C/Windows in the dim and distant past.
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Dec 2014 14:46
Namespace in AppGameKit Basic is fairly abstract, in fact it's so abstract that it's not really even a thing. We can use our own formats for naming variables and that's about it... it's not as if there is any specific namespace handling in AppGameKit - and even if there was, I doubt that many people would actually use it, or at least use it properly.

I am the one who knocks...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 2nd Dec 2014 14:48
I think gosub is a nice simple method of separating "in-line" code to reduce repetition. I hardly use it myself except for initialising other parts of my code. As you said Jim it doesn't seem right initialising anything "global" inside a function but I feel more comfortable doing so inside a subroutine because I am not actually creating anything out of scope.

It might not be great programming but this is a basic language and as such it should be kept simple.

Function = A self contained code structure that performs a "function". Can receive and return any data required to perform its function.

Sub-Routine = A code structure that can be used anywhere to run a "routine" task. Does not receive or return any data but may affect data that is in-scope / global.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 2nd Dec 2014 16:00
What I meant by "name space" was the included files (modules) should be able to expose and protect stuff (private and public) and be addressed with at least a dot prefix.

I don't think AppGameKit Basic is simple, Bax - I think it's confusing. There is no proper distinction between the meaning of global and local, for example, and there's no way to encapsulate things in a unit or module.

With that distinction, gosub would properly be consigned back to the 1970s where it belongs.

-- Jim - When is there going to be a release?
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Dec 2014 16:32
For the heavier Basic programming, I prefer PureBasic - it doesn't do anything that you don't tell it to do!
Like, to access an array with a function in PureBasic, you have to ask to share it, politely!
I kinda like that strictness, where nothing is assumed and the programmer is in total control of everything, and has nobody to blame but themselves if things don't work. I guess the problem is that all these practices have little impact on modern hardware, AppGameKit doesn't care if you want to access 1 array or 100 arrays - there is no real difference between accessing 1 array, and accessing 1 array out of 100.

I do like the way v2 is leaning though, I think it at least promotes sensible variable handling, with it's warnings and syntax checking etc - seems to make sense to give declerations more thought than with v1.

I am the one who knocks...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 2nd Dec 2014 17:14
Jim, I agree that a strict global/local system would make things a lot simpler and easier. I'm not saying AppGameKit Basic is simple, I'm saying it should be. Adding oop concepts might seem simple to someone who understands them or uses them daily but to a new programmer it is very confusing. Just terms like "encapsulation" and "polymorphism" are overly confusing for people still struggling with simple concepts like the difference between "sprites" and "images". Check some of the questions on the forum.

AGK Basic is supposed to be for people with little or no programming experience upwards and I think it does that pretty well. More experienced programmers tend to prefer to use more established languages or write natively using the libraries anyway.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 2nd Dec 2014 19:21
Bax, it's also true that many people have found mistakes in their previous programs since a bit of strict-typing came in!

I think the Basic has improved greatly in the new version, and I applaud that. It seems to me to be a shame that with a few additional tweaks it could be capable of making much more resilient big programs. Nothing would force people to use a more modular approach, but it would enable them to use modules produced by advanced programmers like you in a kind of "black box" way. Instead of lobbing huge amounts of globals in it would be good, surely, at least to have things which were global as far as a particular included file was concerned, but not visible outside that.

-- Jim - When is there going to be a release?
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 2nd Dec 2014 19:29 Edited at: 2nd Dec 2014 19:31
I used to use Gosub for defining my UDTs because you can\'t define them inside a function (AGK & DBP).
However, I recently (last week!) discovered that you don\'t need to do that at all.

UDTs behave much like #constant, in that the code defining them never has to be executed. They just have to appear somewhere to be declared.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Dec 2014 21:41
Quote: " it's also true that many people have found mistakes in their previous programs since a bit of strict-typing came in!"


I'm in that group, and I'm thankful for the stricter typing. It highlighted a couple of bugs in my own that I'd worked around.# assuming the were AppGameKit bugs.

Quidquid latine dictum sit, altum sonatur
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 2nd Dec 2014 21:52
Quote: "We can declare an array globally or locally, but all arrays are treated as global, so it's redundant."


I think that's not true... if I understand that right.

You can create an temporary array wich is restricted to an function... not?

Example:


29 games
19
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 2nd Dec 2014 22:38
I have to admit that I use gosubs all the time and I'll also admit that I don't think it's better, it's just how I prefer to code.

However, I think if I end up having to declare a long list of globals to get the code to work then I don't see much difference between functions and sub-routines.

I do use functions but generally keep them to ones where the variables have to be passed to them, with a single value being returned if appropriate, that way they are truly modular and I can just lift them from program to the next without having to make sure I don't have conflicting global variables.

I never call subroutines from within functions, I've never done it and don't know the consequences of doing so, but I do sub-routines from within sub-routines and functions from within functions.

I did try to make a game using only functions but realised I was becoming more concerned with trying to code "properly" than I was making the game so I just did what I was comfortable with.

I agree with Baxslash and think it's important to keep the tier 1 language as simple and forgiving as possible (yes, I'll also agree that it's not entirely clear what global and what isn't but I just assume that TGC have a good reason for doing things like that). BASIC is supposed to be a beginner language, it's what the B stands for. OK, it might teach a few bad habits but it's better to learn a few of those than to learn nothing at all.

BASIC was also designed for people who understand maths and physics (or whatever) but have no time or inclination to learn a more complicated or demanding language. This is the camp I fall into, I like that I can take stuff I've learned - such as springs and pendulums - and easily code it into a game or demo.

If AppGameKit tier 1 became too strict then I'd probably find it too much of a faff and stop using it or be less inclined to use it.

Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 2nd Dec 2014 22:48
A couple of people raised the issue of efficiency. However I don't think we should let this guide our usage of subroutines. In principle, the difference between them efficiency wise is trivial. In a compiled language, we're talking about maybe 3 assembly instructions difference. As AppGameKit BASIC is an interpreted language, it is entirely possible that there could have been a quirk of the implementation that made a big difference between them. However my rudimentary tests in AppGameKit V2 suggest that the difference between them is almost undetectable.

If you want to use subroutines for some personal stylistic reason, that's fair enough. What I think I'm saying is that I can't see any way in which it would actually be better practise to use a gosub instead of a function.

Although it's getting a little off topic, the OO debate is interesting. As far as I am concerned, now that passing types by reference is available, you can write fully OO code in AppGameKit BASIC. I personally dislike the introduction of OO notation into the language. For a simple language, I want one good way to do each thing (another reason I object to subroutines - they provide an unnecessary alternative to functions). For example, the array.length notation seems very arbitrary, given that for everything else in the language, you pass it into an inbuilt function. Therefore I would have considered Length(array) to be more consistent.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Dec 2014 00:37 Edited at: 3rd Dec 2014 00:41
"you can write fully OO code in AppGameKit BASIC"

Lucas - I don't think so, unfortunately. Passing by value or reference was in Pascal and C long before OOP arrived.

Ultimately, there is no magic either way. But making a very big game system in AppGameKit Basic is going to be a nightmare because you can't make and destroy things as needed. It's fine for little things. I've been introducing my 9 year old grandson to programming using it.

If you are going to create little things, I can't see why using gosub is a big problem. The big issue is that anything you define is likely to stick around for ever.

-- Jim - When is there going to be a release?
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 3rd Dec 2014 01:47
i use to use gosubs.... infact My Windows Music App is all of a one page program with probably stack corruption and everything...lol!

i think [gosub/return/goto] should stay cause its a process of learning.

once you learn how you can loop your program around and around... then you can graduate and move on to functions....

then once you have learned function usage mixed with globals... yes jim i said globals...lol then you can move up in the world to
udt's

and im not there yet... but i can code my arse off using functions with global memory returns

Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 3rd Dec 2014 09:09
Quote: "Lucas - I don't think so, unfortunately. Passing by value or reference was in Pascal and C long before OOP arrived."

Sorry I don't think I was clear there. I don't mean that I think any new OO language features will be added to AppGameKit BASIC (I have no idea what TGC's plan are). I just mean that with the tools available now, you can write code using the OO paradigm (as you can in C - I have no experience of Pascal). It's possible to write OO code in a language without OO support, just as it is possible to write entirely imperative code in a language like Java that does everything in its power to ram OO down your throat.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Dec 2014 10:32
So how do you declare a type with methods in it without jumping through a lot of hoops?

-- Jim - When is there going to be a release?
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 3rd Dec 2014 10:50
You can't. I'm not even sure that AGK2 isn't entirely static which precludes polymorphism and virtual functions (though you can fudge it).

Personally my proposed solution is a bit of syntactic sugar in the compiler. When it comes across a function invocation like:

someObject.someMethod(42)

where someObject is of someType it should rewrite this to be:

someType_someMethod(someObject,42)

which it can do because the compiler must know what type someObject is at this point, and the method is defined as

function someType_someMethod(self ref as someType, p1 as integer)

Super/Sub classing could be done as types within types. This is all statically linked at compile time stuff, but it does mean you can encapsulate a bit more.

I am considering writing a processor which maintains these with the method invocation in inline functions e.g.

//$ someObject:someMethod(42)
someType_someMethod(someObject,42)


so a sort of preprocessor except the source and compiled code are in the same file - the second line is maintained by the "preprocessor" but there are no multiple source files.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Dec 2014 11:35
That's an interesting approach!

I like hitting my head against the wall with problem like this. What I had in mind was a jump table and a "tiny" compiler that can run at program start and fix up the definitions, possibly putting its own byte-code into a memblock.

We could - for fun - then have the equivalent of the Lisp Eval operation, which allows a program to write code and then execute it. I've done this for Genetic Programming, where you evolve maybe 500 functions and test them for fitness, cross-breeding and mutating for many generations until you get close to an answer.

Of course, in the real world I will use AppGameKit for Pascal to add enormous power out of the box. But as I said - it's a fun challenge.

-- Jim - When is there going to be a release?
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 3rd Dec 2014 12:46
I used something very similar a few years ago when I wrote a Berzerk clone in Z80 Assembler on a TI Calculator. It's some and some ; it undoubtedly helps with encapsulation and organisation, but it doesn't really do polymorphism because everything is static.

Of course if Paul would give us the bytecode definitions we could just write a back end ....
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Dec 2014 13:25
They're in the C source: AGKCommands.h and AGKCommandsTable.h

So - if we could fix up Gosub to jump to a patch of code in a memblock we could, effectively, have plugins. Or objects.

-- Jim - When is there going to be a release?
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 3rd Dec 2014 15:28
Didn't know that. Answers the original question, gosub is marginally more efficient as there isn't a stack frame.

Looking through it, it looks like AGK2 can't (at present) do an indirect branch, except by cheating. The interpreter tracks GOSUB/RETURN (so if you do return without gosub it barfs) but it doesn't track AGKI_FUNCTION_CALL and AGKI_FUNCTION_EXIT so you could, theoretically push a fake stack frame on to do it. But it's a huge hack.

I think it's a good thing that the bytecode interpreter code source is available. One concern about any non O/S project is that the company might withdraw the product. The problem is, especially iOS that the build is a moving target. Some companies struggle a bit ; Corona have just been taken over, Gideros have open sourced, Monkey-X has semi open-sourced after its coder posted a note which stated he wasn't earning enough, etc.

Login to post a reply

Server time is: 2024-11-25 11:39:32
Your offset time is: 2024-11-25 11:39:32