global DIMs

new BookmarkLockedFalling
veneff
New Member
*

veneff Avatar

Posts: 29

Post by veneff on Jan 15, 2009 17:15:24 GMT -5

I am trying to understand what limitation there may be with the DIM statement.

Any DIMed variables are automatically global.
But it seems as if you can't declare a DIMed variable in a SUBroutine. It seems to act as if it is local to that routine, ie:


CALL MakeArray

FOR I = 1 to 20
Array(I) = I
NEXT I
END

SUB MakeArray
DIM Array(20)
END SUB

causes error:
Runtime Error in program 'untitled': Array(I) = I
Message not understood: #value:

And apparently you can not reDIM a variable in a subroutine and expect to access the increased size of it in the main portion of the program, ie:

DIM Array(10)

CALL MakeArray

FOR I = 1 to 20
Array(I) = I
NEXT I
END

SUB MakeArray
DIM Array(20)
END SUB

causes error:
Runtime Error in program 'untitled': Array(I) = I
Index out of bounds: 11

Vance
Last Edit: Jan 15, 2009 17:21:52 GMT -5 by veneff
StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

Post by StefanPendl on Jan 17, 2009 5:46:41 GMT -5

Any DIM in the main part of the program creates a global array.
Any DIM inside a procedure (function and sub, not gosub) creates a local array.

I noticed that the REDIM command is missing from the command reference, but it does not change the size of a global array too.
[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM