Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Using a procedure or a gosub function ?  (Read 3718 times)

0 Members and 2 Guests are viewing this topic.

Tarzin

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 35
    • Ami-France
Using a procedure or a gosub function ?
« on: November 15, 2007, 09:08:29 AM »

Hello,

I'm not very familiar with procedures in Amos Pro.
It's better to use a procedure or a function Gosub / Return ?

In wich case are procedures better ?

I'd like to code a function to delete tiles on the screen (this will be used 96 times in a game)
Which solution could be better ?
Thanks for your help!
« Last Edit: November 15, 2007, 09:11:32 AM by Eric LUCZYSZYN »
Logged
Power User! :-)
A500/1Mo + 2nd floppy drive

WinUAE 1.5.x / OS 3.9 BB2 / Amikit 1.x.
http://amifrance.hw.tc/

AmiGame, Ultimate FrontEnd for WinUAE!
http://www.amigame.fr/

Spellcoder

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 27
    • Amiga Coding WIKI
Re: Using a procedure or a gosub function ?
« Reply #1 on: November 15, 2007, 03:35:12 PM »

Procedures:
- calling a procedure takes a little bit more time than doing a gosub
- you can fold procedures, which makes your sourcecode easyer to read

Gosubs:
- faster to call


So I would say:
- use procedures for parts of code that are large, not called often in long loops, or take a lot of time (so the calling of the procedure uses only a little % of the time)
- use gosubs for small pieces of code or code that is used often in loops


96 times isn't a very long for..next loop. If the code you need to call is large I'd put it in a procedure.
If that code you use 96 times is short I would use it with a gosub. (or maybe in the for..next loop if it's really short).
« Last Edit: November 15, 2007, 03:39:44 PM by Spellcoder »
Logged

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com
Re: Using a procedure or a gosub function ?
« Reply #2 on: November 15, 2007, 08:37:13 PM »


I agree with what Spellcoder says. There are instances where Procedures are better than a subroutine (Gosub... subroutine... Return), however he forgot one vital thing:

 - Any variables used outside of the procedure that you wish to be used inside (or vice versa) need to be globalised.


For this reason I personally prefer using subroutines. Also, additional glitches can occur when using Procedures too, such as Data statements at the end of the program (and after procedures) aren't always recognised when you run your program - though you should really be putting data into Amos Banks anyway :)


Regards,
Lonewolf10

Logged

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: Using a procedure or a gosub function ?
« Reply #3 on: November 15, 2007, 10:36:07 PM »

curious , i tend to use loads and loads of procedures, because it makes the code that much easier to read / debug

right down to sometimes 2-3 lines in a procedure!

does anyone really think there's myuch difference in speed, only this might change my coding style quite a bit?



p.s. hi guys, been a bit busy of late but still trying to check in ;)
Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: Using a procedure or a gosub function ?
« Reply #4 on: November 15, 2007, 10:54:18 PM »

One advantage to procedures is that, since they support "local variables", it is possible to use them with recursive routines like flood fill and Mandelbrot fractals and so on.

As far as I'm concerned, GOSUB should be considered DEPRECATED.  If you only want it to work in AmosPro then go ahead and use it, but in Mattathias BASIC some commands will likely have to be dropped simply because they have no C++ equivalent.  (After all, if we use C++ as a backend, it had better be able to reproduce most of the functionality of the original langauge.)  BTW, in C++ there is no calling overhead in a procedure that does no parameter passing so it won't be any faster to use GOSUB in the future anyway.
Logged

Tarzin

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 35
    • Ami-France
Re: Using a procedure or a gosub function ?
« Reply #5 on: November 21, 2007, 09:20:47 AM »

Thanks a lot for your explainations!

I've coded many sub-routines using a gosub function. Working fine!
I'll try later to use procedure
Logged
Power User! :-)
A500/1Mo + 2nd floppy drive

WinUAE 1.5.x / OS 3.9 BB2 / Amikit 1.x.
http://amifrance.hw.tc/

AmiGame, Ultimate FrontEnd for WinUAE!
http://www.amigame.fr/
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022