GLBasic forum

Main forum => GLBasic - en => Topic started by: bigtunacan on 2011-Oct-09

Title: Syntax question?
Post by: bigtunacan on 2011-Oct-09
I'm curious why do some of the built in commands work in the same manner as a function call using the brackets like

Code (glbasic) Select

SPRCOLL (5, 0, 0, 6, 0, 0)


but then others only work without the brackets like

Code (glbasic) Select

DRAWSPRITE 6, 0, 0


This seems very inconsistent and I don't see a clear reason why/when each is being used?
Title: Re: Syntax question?
Post by: Omadan on 2011-Oct-09
The only thing I can think of there is that the SPRCOLL is a comparison. if(xxxx) then do xxxxxxx
whereas the DRAWSPRITE is just a simple draw command. Not 100% sure though, its better to let Gernot shed some light here :P
Title: Re: Syntax question?
Post by: Moebius on 2011-Oct-09
Functions with return values need brackets (by convention at least), but it almost makes sense to have commands that don't return values specified without brackets.  It's BASIC, not C++  ;)
Title: Re: Syntax question?
Post by: Moru on 2011-Oct-09
What I learned about Basic when I started was that functions that returns values has (), functions (or commands) that don't return values has no ().

eg:
x=ASL(y)
PRINT "hello"
Title: Re: Syntax question?
Post by: bigtunacan on 2011-Oct-09
@Moru,

If that was consistent it would at least then make some sense systematically; however if I define a function in my code I must always call it using bracket notation as far as I can tell, so still this does not seem consistent.
Title: Re: Syntax question?
Post by: MrTAToad on 2011-Oct-09
Dont forget that all user-defined GLBasic functions return a value - you can choose to ignore it or not...
Title: Re: Syntax question?
Post by: Moru on 2011-Oct-10
Functions - Returns value
Subs - Does not return value

Function call:
x = my_function()

Sub call:
GOSUB my_function

Perfectly consistent to me. If you choose to ignore the return value it's up to you to write it differently.
Title: Re: Syntax question?
Post by: bigtunacan on 2011-Oct-10
@Moru,

See my original post using DRAWSPRITE as an example.  It s not called using GOSUB, but it uses non-bracket notation.
Title: Re: Syntax question?
Post by: AMateus on 2011-Oct-10
Hi,

I don't see any inconsistency in GLB syntax wise.

Every command that return a value has brackets, any command that doesn't return a value, doesn't have brackets. It's the difference between Functions ans Procedures.

It's a basic concept: Procedures execute an action and that's it, Functions executes an action and return something. For example, by Visual Basic 6 uses this approach.

DRAWSPRITE doesn't return a value, so it's not supposed to have brackets. DRAWSPRITE executes an action and that's it. If it returned, for example, a success of failure result, then you had to use brackets.

António
Title: Re: Syntax question?
Post by: matchy on 2011-Oct-10
It's a good point and I don't usually don't create subroutines in glb either because they could also return useful errors. For example, DOESFILEEXIST() could be incorporated into LOADSPRITE(), which is the function that I end up using/creating anyhow.


Title: Re: Syntax question?
Post by: Moru on 2011-Oct-11
Quote from: bigtunacan on 2011-Oct-10
@Moru,

See my original post using DRAWSPRITE as an example.  It s not called using GOSUB, but it uses non-bracket notation.

The post I commented was talking about functions you yourself defined and called. DRAWSPRITE is an internal command to draw a sprite. Does not return anything so has no brackets.
Title: Re: Syntax question?
Post by: erico on 2011-Oct-11
interesting subject,

I never really bothered for it or even notice it. I only ever used basic and veeery little assembler.
But now I remember trs80color basic, line command had brackets for coordinates...? Line(0,0)(10,40),1 Draw command didn´t.

really curious!

Title: Re: Syntax question?
Post by: bigtunacan on 2011-Oct-11
My original  post specifically called out the internal commands.  Taking it a step further however, IMHO, the style used for internal commands and user defined commands should always match.  I have no issue with the difference between a user defined function and a user defined sub.  That is consistent and causes no confusion.  The one that will cause me to stumble from time to time is the internal commands.  I will be happily coding away and then I get a compile error because I called something internal using a standard functional notation.  While these things are by no means deal breakers, it causes you to stumble for a second and can get you out of the zone.  In most languages I have worked with there is no difference between how internal and user defined functions are called from the code. 

If the issue truly is that you don't need brackets if a value is not returned as a couple of people stated that would at least then make some sense, but it would beg the question why I am unable to do the same thing with a user defined function?  There are times when I have functions that are executing based on my parameters provided and I have no care about anything returned.

I was surprised to get so many responses on this, but I'm glad to see that at least other people care about this as well whatever your opinions may be. :)
Title: Re: Syntax question?
Post by: Slydog on 2011-Oct-11
As mentioned by AMateus, Visual Basic 6 uses the same method as GLBasic.
What's even more confusing, VB6 allows both, depending on the context you call the function.
If using MsgBox() to only prompt the user you don't need the brackets, but if you want to check the button clicked, you NEED the brackets!

GLBasic user functions ALWAYS return something (whether you check for it or not), because when you don't specify a return type, float (#) is assumed.  Well, that's my guess anyways.

However, I would personally prefer ALWAYS needing brackets, to make my code consistent.
I occasionally use the DEBUG command (no brackets) but also use my own custom 'Log()' function and always get the brackets mixed up between the two!
Title: Re: Syntax question?
Post by: matchy on 2011-Oct-12
Quote from: erico on 2011-Oct-11
interesting subject,

I never really bothered for it or even notice it. I only ever used basic and veeery little assembler.
But now I remember trs80color basic, line command had brackets for coordinates...? Line(0,0)(10,40),1 Draw command didn´t.

really curious!

Oh yeah! I have noticed that during some coco graphics coding recently. ;) It also remind me of good ol' "m=peek(n)" and "poke n, m"
Title: Re: Syntax question?
Post by: erico on 2011-Oct-12
Hey matchy, I´m curious about this coco coding of yours, I remember an old post where you asked if anyone was into retro-coding. Can you share something visual about that? a picture maybe?

Dealing colors on coco was really something, specially on a TV.
I still have some rainbow magazines (I use to buy it and could not even read it as I knew no english by them) where they had a galley section, most of the images where done on coding, outstanding stuff...later coco max (first color photoshop worldwide?) got on the way, but still, really impressive stuff.

...I also remember a part where they had single line code showoff, people would post their solutions or full stuff coded into a single line of basic...heck, my kid´s mind was definently fried by that...reminds me a little on the Tiny Cursor Key Snippet spicepixel posted recently. This kind of optimization I guess requires too much math and logic..I wish I could.
Title: Re: Syntax question?
Post by: matchy on 2011-Oct-12
Sure Erico (and I'm not sure if I should start another thread).  :-[

I've been experimenting with sprites and map scrolling. The Rainbow and Coco Max magazine were great and the pdfs are available to download. Coco Max is also an inspiration to recreate in glb. ;) Here is an example although I need to find my PMODE and sprite GET/PUT tests.

The way I go about it is that I type it in notepad then paste it in coco MESS emulator.
Code (glbasic) Select

' Tandy TRS-80 Color Computer BASIC
' Simple text map scrolling test

NEW
CLS

5 CLS 0

10 ' INIT
20 DIM DT$(16)
30 MX = 0
40 MY = 0
50 GOSUB 900 ' READ DATA
60 GOSUB 500 ' ANIMATE

500 ' ANIMATE
510 CLS 0
520 GOSUB 600 ' CALC MAP
530 GOSUB 700 ' DRAW MAP
580 ' GOSUB 1100 ' PAUSE
590 GOTO 500 'RETURN

600 ' CALC MAP
610 MX = MX - 1
620 IF MX < -25 THEN MX = 0
630 RETURN

700 ' DRAW MAP
710 FOR Y = 2 TO 15
720  FOR X = 0 TO 9
730   TM$ = MID$(DT$(Y + 1), X + 1 - MX, 1)
740   IF TM$ = "A" THEN TM$ = CHR$(127 + 32) : GOSUB 800 ' PRINT TILE
750   IF TM$ = "X" THEN TM$ = CHR$(127 + 16) : GOSUB 800 ' PRINT TILE
760  NEXT
770 NEXT
780 RETURN

800 ' PRINT TILE
810 PS = (Y * 32) + X
820 PRINT @PS, TM$;
830 RETURN

900 ' TEST
910 FOR N = 0 TO 15
920  READ DT$(N)
930 NEXT
940 RETURN

1000 ' DRAW MAP 2
1010 FOR Y = 0 TO 15
1020  PRINT @ Y * 32, "                   ";
1030 NEXT
1040 RETURN

1100 ' PAUSE
1110 FOR I = 0 TO 9000 : NEXT
1120 RETURN

5000 ' DATA
5010 DATA ".........................................................."
5020 DATA "...................................................AA....."
5030 DATA ".....AA............................................A......"
5040 DATA "........................................A..........A......"
5050 DATA "....................A...................A................."
5060 DATA "....................AAA.................A..........A......"
5070 DATA "......................A..................................."
5080 DATA "........AAAAA................................AA..........."
5090 DATA "........AAAAA...............................AAAA.........."
5100 DATA "............................................AAAA.........."
5110 DATA ".............................AAA.........................."
5120 DATA ".............................AAA.........................."
5130 DATA ".............................AAA................AAA......."
5140 DATA "................AAAA......................................"
5150 DATA ".......A........AAAA......................................"
5160 DATA "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
5170 DATA "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
5180 DATA "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

RUN
Title: Re: Syntax question?
Post by: Kitty Hello on 2011-Oct-12
I got a bit confused now, but matchy corrected my mind.
My basic understanding of "BASIC" comes from my C64 days, where functions with no return are called w/o braces:
Code (glbasic) Select

POKE 1024, 88
REM check left, top corner now
PRINT PEEK(1024)


See also the examples on the Wiki
http://en.wikipedia.org/wiki/BASIC (http://en.wikipedia.org/wiki/BASIC)

So, it's not inconsistent, it's BASIC.
Title: Re: Syntax question?
Post by: erico on 2011-Oct-12
Matchy,
I was expecting a picture...you know, pictures tell a thousand words...
but then you gave code, well :zzz: codes tell a thousand pictures! :)

I almost thought I would have to type all that on the mocha emulator just to fulfill my curiosity!
I knew better to follow the hint on paste code on mess..

after a few tries, damn it, it goes pasting as if someone is typing the thing! my mind is just busted, I did that my self a lot as by that time I was 10 years old :O

I will just have to paste a pic of one of the runs, it´s clear that this picture speaks 1 billion words!
heck, I´m really happy now, thanks!

ps: no poke for speed, but I get the deal from the code, I´m impressed, if I only knew this by the time I wrote my ninja giraya game...
Most of my games were characters made, a few had get/put, but memory was soooooo short for that and I knew nothing how to optimize or tile it... I succeed in only one, a game where things fell from a tree, you had to collect fruits, 6 short sprites made the deal...I wish I had the cassete with this code.

ps2: where is the time bandit remake?! I wanna see that!

[attachment deleted by admin]
Title: Re: Syntax question?
Post by: matchy on 2011-Oct-12
I think the whole purpose of BASIC is to create an understandable form that could be simply understood by anyone unlike c or Pascal. In this example, even though brackets are used, it makes sense to use brackets to clearly distinguish that there are two points. Technically, this aspect seems to naturally override proper efficient forms in some cases.

eg1: LINE(0,0)-(128,128)
eg2: LINE 0, 0, 128, 128

erico, wow that is cool you tried it. I paste on maximum emulator speed, so it looks like 200 words per minutes are being typed. lol.


Title: Re: Syntax question?
Post by: ampos on 2011-Oct-12
To the ones without any other language knowledge, just the old-good Basic, it is clear: brackets=returns a value. No brackets, does not.

Title: Re: Syntax question?
Post by: ampos on 2011-Oct-12
Quote from: Kitty Hello on 2011-Oct-12
I got a bit confused now, but matchy corrected my mind.
My basic understanding of "BASIC" comes from my C64 days, where functions with no return are called w/o braces:
Code (glbasic) Select

POKE 1024, 88
REM check left, top corner now
PRINT PEEK(1024)


See also the examples on the Wiki
http://en.wikipedia.org/wiki/BASIC (http://en.wikipedia.org/wiki/BASIC)

So, it's not inconsistent, it's BASIC.

Title: Re: Syntax question?
Post by: bigtunacan on 2011-Oct-13
Ok, so if the consistency is that brackets is for return value, and no brackets is no return value, then is there a way for me to create user defined functions that do NOT return values?
Title: Re: Syntax question?
Post by: Moebius on 2011-Oct-13
SUB-routines are essentially that:
Code (glbasic) Select
GOSUB MySub
END

SUB MySub:
     PRINT "...", 0, 0
ENDSUB
Title: Re: Syntax question?
Post by: bigtunacan on 2011-Oct-13
@Serpent,

No, Sub routines do not accept parameters; it is not at all the same thing as a function that does not return a value.
Title: Re: Syntax question?
Post by: Kitty Hello on 2011-Oct-13
no, that's not possible.
Title: Re: Syntax question?
Post by: Moebius on 2011-Oct-13
... "essentially"...  :whistle:
The only alternative is to ignore the returned number... is that so bad?
Title: Re: Syntax question?
Post by: matchy on 2011-Oct-13
Errors returned are helpful. Eg:
Code (glbasic) Select

// Hypothetical GLB code
LOCAL out_of_drawbuffer_bounds = DRAWSPRITE(sprite_player, player_x, player_y, check_for_bounds = TRUE)


Title: Re: Syntax question?
Post by: Ruidesco on 2011-Oct-13
Quote from: bigtunacan on 2011-Oct-13
Ok, so if the consistency is that brackets is for return value, and no brackets is no return value, then is there a way for me to create user defined functions that do NOT return values?
Functions do always return a default value when you don't specify any.