Subroutines

GFA BASIC-related articles in here please

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
paul92706
Atari God
Atari God
Posts: 1341
Joined: Mon Apr 25, 2011 4:28 am
Location: Orange, CA

Subroutines

Postby paul92706 » Thu Jun 20, 2013 4:14 pm

Hi guys, i got a question, what is the difference between PROCEDURE and FUNCTION subroutines? thanks!
Atari Falcon CT60/CTPCI 14MB+ 512mb ATI9250 + NetUSBee
Atari Falcon030 CF 4gb + NetUSBee+ 14MB Ram
Atari TT030 2meg STRAM/16meg TTRAM + Nova Adaptor +Maxtor SCSI HD + DaynaPort Pocket SCSI

Dal
Administrator
Administrator
Posts: 4066
Joined: Tue Jan 18, 2011 12:31 am
Location: Cheltenham, UK
Contact:

Re: Subroutines

Postby Dal » Thu Jun 20, 2013 4:26 pm

Use a function if you want a value returned to the calling routine. Otherwise use a procedure.
TT030: 4MB/16MB + Crazy Dots, Mega"SST" 12, STacy 2, MegaSTE, STE: Desktopper case, IDE interface, UltraSatan (8GB + 512Mb) + HXC floppy emulator. Plus some STE's/STFM's

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4774
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: Subroutines

Postby simonsunnyboy » Thu Jun 20, 2013 4:52 pm

It's not 100% true, a PROCEDURE can return values too., via global variables or pointer parameters.
It is more clean to use FUNCTION tough, available from GFABASIC v3 and up.
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

Dal
Administrator
Administrator
Posts: 4066
Joined: Tue Jan 18, 2011 12:31 am
Location: Cheltenham, UK
Contact:

Re: Subroutines

Postby Dal » Thu Jun 20, 2013 7:40 pm

Setting a global variable is not the same as returning a value - anything can modify a global variable. While this isn't so important with GFA on Atari, it breaks down when you apply the same 'understanding' to modern BASICs. Using Global Variables should be handled with care in my opinion. Obviously you have no choice if you need to "return" a string - GFA only returns numerical values.

Also you can't do this with a procedure:

Var1 = function1(function2(var1), var2, function3(var2))
TT030: 4MB/16MB + Crazy Dots, Mega"SST" 12, STacy 2, MegaSTE, STE: Desktopper case, IDE interface, UltraSatan (8GB + 512Mb) + HXC floppy emulator. Plus some STE's/STFM's

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2228
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: Subroutines

Postby lp » Thu Jun 20, 2013 7:56 pm

Actually, you can write string functions in GFA. :wink:

Code: Select all

test$=@add$("GFA","BASIC")
PRINT test$
'
FUNCTION add$(a$,b$)
  RETURN a$+b$
ENDFUNC

Dal
Administrator
Administrator
Posts: 4066
Joined: Tue Jan 18, 2011 12:31 am
Location: Cheltenham, UK
Contact:

Re: Subroutines

Postby Dal » Thu Jun 20, 2013 8:23 pm

Interesting, my interpreter (3.4 or 3.5 - I'll have to check) borked when I tried this.
TT030: 4MB/16MB + Crazy Dots, Mega"SST" 12, STacy 2, MegaSTE, STE: Desktopper case, IDE interface, UltraSatan (8GB + 512Mb) + HXC floppy emulator. Plus some STE's/STFM's

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2228
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: Subroutines

Postby lp » Thu Jun 20, 2013 8:30 pm

It might be that you left the "$" off the function name? GFA looks at the RETURN xxx line and if its a string being returned it will throw a syntax error if the "$" is missing from the function name.

Will this discovery lead to the robot software being re-optimized? :)

Dal
Administrator
Administrator
Posts: 4066
Joined: Tue Jan 18, 2011 12:31 am
Location: Cheltenham, UK
Contact:

Re: Subroutines

Postby Dal » Thu Jun 20, 2013 8:39 pm

Yup - I stand corrected. Thanks for the tip!

Pretty much everything in the Robokit engine is chucking numbers around. Globals are only used for setting up or changing the behaviour of the engine itself.

I might need to have another quick look at the error handler though...
TT030: 4MB/16MB + Crazy Dots, Mega"SST" 12, STacy 2, MegaSTE, STE: Desktopper case, IDE interface, UltraSatan (8GB + 512Mb) + HXC floppy emulator. Plus some STE's/STFM's

User avatar
paul92706
Atari God
Atari God
Posts: 1341
Joined: Mon Apr 25, 2011 4:28 am
Location: Orange, CA

Re: Subroutines

Postby paul92706 » Thu Jun 20, 2013 9:11 pm

lp wrote:Actually, you can write string functions in GFA. :wink:

Code: Select all

test$=@add$("GFA","BASIC")
PRINT test$
'
FUNCTION add$(a$,b$)
  RETURN a$+b$
ENDFUNC

that subroutine parameter settings look similar to DEFFN/FN func, commands.
Atari Falcon CT60/CTPCI 14MB+ 512mb ATI9250 + NetUSBee
Atari Falcon030 CF 4gb + NetUSBee+ 14MB Ram
Atari TT030 2meg STRAM/16meg TTRAM + Nova Adaptor +Maxtor SCSI HD + DaynaPort Pocket SCSI

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2228
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: Subroutines

Postby lp » Fri Jun 21, 2013 2:19 am

paul92706 wrote:
lp wrote:Actually, you can write string functions in GFA. :wink:

Code: Select all

test$=@add$("GFA","BASIC")
PRINT test$
'
FUNCTION add$(a$,b$)
  RETURN a$+b$
ENDFUNC

that subroutine parameter settings look similar to DEFFN/FN func, commands.


DEFFN allows you to define a single line function where as the multi-line functions can be far more complex.

Code: Select all

test$=@add$("GFA","BASIC")
PRINT test$
'
DEFFN add$(a$,b$)=a$+b$

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4774
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: Subroutines

Postby simonsunnyboy » Fri Jun 21, 2013 3:34 pm

I want to add that mentioning global variables were not meant to promote their use. They can't be avoided in GFABASIC in some cases but I learned the hard way that with a certain project size, keeping track of them is a pain in the a**.
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org


Social Media

     

Return to “GFA BASIC”

Who is online

Users browsing this forum: No registered users and 1 guest