Ultimate Amiga

Please login or register.

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

Author Topic: Problem with dimensioning arrays in a subroutine  (Read 4225 times)

0 Members and 2 Guests are viewing this topic.

Brick Nash

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 83
  • Generic Amiga User
Problem with dimensioning arrays in a subroutine
« on: December 20, 2020, 06:15:37 PM »

Hi,

Does anyone know if there is a problem with dimensioning arrays in a subroutine?

I define all my variables in a place at the bottom of my code and I call it with a Gosub at the start of my program to initialise everything. It works fine with regular variables but, it doesn't seem to work with arrays. It keeps telling me that my arrays haven't been dimensioned, but they absolutely have, and they are all global arrays too. Also, my procedures don't seem to recognise the arrays either, again even though they are global.

There's nothing in the manual about this. It just tells you how to dimension an array, but it doesn't say where it has to be done or if there are any problems with doing it in certain places. I've checked my code about six times only to find nothing wrong, so I'm kind of at a loss as my arrays seems to be getting ignored.

I can print the value of an array offset if I'm within the subroutine, but anything outside just throws up an "Array not Dimensioned" error.

I'm genuinely stumped, so any help would be appreciated!
Logged

adrazar

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 67
  • Generic Amiga User
Re: Problem with dimensioning arrays in a subroutine
« Reply #1 on: December 20, 2020, 10:12:29 PM »

You just have to move the Dim statements up to the top of your program, and the Global statements as well. But the remaining steps of the initialisation is fine to keep in a Gosub.
Logged

adrazar

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 67
  • Generic Amiga User
Re: Problem with dimensioning arrays in a subroutine
« Reply #2 on: December 20, 2020, 10:54:05 PM »

To be a bit more clear on why the program tester complains, consider the following:
Code: [Select]
Gosub INIT
'Print A(3)   <- uncommenting this line results in an error
Goto E

INIT:
Dim A(5)
Return

E:
'Print A(3)   <- uncommenting this line results in no error
Hence the problem isn't that the Dim is inside a Gosub, it is really a matter of the order of the statements in a line number sense; if a program says to access an array element at some line in the program, then the Dim instruction of the array must be located somewhere physically above, or the testing won't pass.
Logged

Brick Nash

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 83
  • Generic Amiga User
Re: Problem with dimensioning arrays in a subroutine
« Reply #3 on: December 21, 2020, 10:02:29 AM »

Thank you for the information.

I was tearing my hair out on this issue as there is nothing in the manual that explains conditions like that. I'm happy to move it to the start of the program, I just though that using a Gosub was tidier.

Could I maybe put them in a procedure as long as the procedure was at the start of the program? That way it could be folded.

Thanks.
Logged

adrazar

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 67
  • Generic Amiga User
Re: Problem with dimensioning arrays in a subroutine
« Reply #4 on: December 21, 2020, 10:25:47 AM »

Could I maybe put them in a procedure as long as the procedure was at the start of the program? That way it could be folded.

I believe you can :D

EDIT: aw, no, the arrays become local to the procedure if you define them there, so you can't hide the definitions like that after all. :/
« Last Edit: December 21, 2020, 10:29:40 AM by adrazar »
Logged

Brick Nash

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 83
  • Generic Amiga User
Re: Problem with dimensioning arrays in a subroutine
« Reply #5 on: December 21, 2020, 10:46:46 AM »

That's odd. In the manual (Page 05.05.07) it says that Arrays can be dimensioned in a Procedure and then made global.

Quote
With AMOS Professional, you are able to define global arrays from a procedure, even if the array is not created at root level, as follows:

X> Procedure VARIABLES
   Dim ARRAY(100,100)
   Global ARRAY()
   End Proc

However, I just tried it out and, as you say, it doesn't work.

EDIT: Tried a lot of different combinations with things in and out of the Procedure, and it just doesn't work at all. Even if the Dim command is right above the Global declaration it still says that the Array is not dimensioned if both of them are within the Procedure. I think this will have to be marked down as a bug and a mistake in the manual.
« Last Edit: December 21, 2020, 11:24:54 AM by Brick Nash »
Logged

adrazar

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 67
  • Generic Amiga User
Re: Problem with dimensioning arrays in a subroutine
« Reply #6 on: December 21, 2020, 11:34:09 AM »

I think this will have to be marked down as a bug and a mistake in the manual.

Yes, I didn't get the wild card thing to work either the time I tried to test it out, so there's definitely something strange about that section of the manual..
Logged

Brick Nash

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 83
  • Generic Amiga User
Re: Problem with dimensioning arrays in a subroutine
« Reply #7 on: December 21, 2020, 02:57:26 PM »

Shame. Anyway, thank you for the help, and have a Merry Christmas or whatever holiday you celebrate. :)
Logged
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022