Page 1 of 1

Possible to store an array in a sub?

Posted: Sat Aug 25, 2007 8:37 pm
by Raspberrypicker
I've been working on a new project, and I've realized that I can't store an array in a sub program and then call it back to the main program so I can use the variables (or at least I don't know how to). So is it possible? I know for a fact that my program runs fine without making a seperate sub for the arrays.

Thanks in advance...even if there is no solution my program will be fine.

Posted: Sat Aug 25, 2007 9:48 pm
by Patz QuickBASIC Creations
You have to DIMension the array before you use it. And then you have to pass it to the SUB. ie:

Code: Select all

DECLARE SUB MySub (Array%())
DIM SomeStuff%(5,5)

MySub SomeStuff%()

SUB MySub(Array%())
FOR A=1 TO 5
 FOR B = 1 to 5
  Array%(A,B) = A*B
 NEXT B
NEXT A
END SUB
See if you can make heads or tails of that, and then post your results ;)

Posted: Sat Aug 25, 2007 11:51 pm
by BadMrBox
The array need's to be Dim Shared if it's gonna be noticed in a sub or function.

Posted: Sun Aug 26, 2007 4:02 pm
by Raspberrypicker
kk, thanx. I think I understand it.

So when I pass it to the sub does this part go into the main program or sub program?

MySub SomeStuff%()

I think that's the only part I didn't do...plus DIM SHARED. So basically when I add SHARED to it, it lets me "share" the array between subs, ok I got it now. :D

Posted: Sun Aug 26, 2007 7:23 pm
by BadMrBox
I didn't noticed that Patz called the sub with the array :roll: , silly me. If you do it like that then you don't need to have the array shared.
But anyway, you can either do like this;

Code: Select all

DECLARE SUB MySub (Array%())
DIM SomeStuff%(5,5)

MySub SomeStuff%() 

SUB MySub(Array%()) 
FOR A=1 TO 5 
 FOR B = 1 to 5 
  Array%(A,B) = A*B 
 NEXT B 
NEXT A 
END SUB 
or like this

Code: Select all

DECLARE SUB MySub
DIM Shared SomeStuff%(5,5)

MySub

SUB MySub
FOR A=1 TO 5 
 FOR B = 1 to 5 
  Array%(A,B) = A*B 
 NEXT B 
NEXT A 
END SUB 
I like the later way rather than the first.

Posted: Mon Aug 27, 2007 1:29 am
by k7
The latter way is incorrect. Shouldn't the loops be from 0 to 5?

Posted: Mon Aug 27, 2007 1:34 am
by Dr_D
Perhaps lbound to ubound?

Posted: Mon Aug 27, 2007 8:43 am
by ThemePark
Actually it should be 0 to 4, since you dimension 5 elements in the array. But lbound to ubound would be better, less hard-coding, although it's not really important in a small program like this.

Posted: Mon Aug 27, 2007 9:20 am
by Joe
Unless you declare OPTION BASE 1, then there are actually 6 elements allocated in the array. This is BASIC, so there's nothing wrong with going from 1 to 5.
Raspberrypicker wrote:I think that's the only part I didn't do...plus DIM SHARED. So basically when I add SHARED to it, it lets me "share" the array between subs, ok I got it now. :D
Don't use SHARED if you don't have to. Pass everything as arguments into the sub. Abusing SHARED can easily lead to spaghetti code. That's my advice, even though I don't follow it all the time. :)

Posted: Mon Aug 27, 2007 7:40 pm
by Raspberrypicker
HOORAY!!! It works!!

Thanx guys, all my arrays are finally in their seperate sub. =)

Now all I gotta work on is my graphics. :(
Man, there are so many bugs in my graphics, its not even funny...guess im gonna be debugging for a while, lol.

Posted: Tue Aug 28, 2007 3:39 pm
by BadMrBox
Hm, in what way are your graphics buggy? Problems with lines and circles and such stuff? Are you using screen13? Then I would recommend PP256.

Posted: Tue Aug 28, 2007 6:18 pm
by Raspberrypicker
nah it's not that. I'm trying to use a bitmap for my title. The only problem is that I forgot how to use bitmaps cuz I only briefly learned it. So I gotta refresh myself with a turtorial. I tried to do it on my own from memory, but I ended up with something screwy.

Besides, there has to be something better than using a bit map for a title, lol...any suggestions??

And yes I am using screen 13. I only need help with my title, i want to make it look cool. All the other stuff is fine, I cud just use lines and colors and that part is good.

Another quick question: How do I know what screen to use for my programs? And what does it mean that screen 7 has pages?

Posted: Tue Aug 28, 2007 6:34 pm
by Joe
It all depends on what resolution you want and how many colors your program/game will support. In QB, most screens only support 16 colors, but if you want more color than go for Screen 13, which is an 8bit graphics mode that supports 256 colors.

Paging means that there is more than just one screen to draw on. The visible page is what you see, and hidden pages are the extra ones. It helps because you can draw all your graphics on the hidden page, and then qucikly copy that page over to the visible page, greatly reducing flicker.

The reason why Screen 7 has paging and Screen 13 doesn't is that the pages in Screen 7 take up less memory than a Screen 13 page. Screen 7 only supports 16 colors (4bit mode), so a screen 7 page only takes up half the memory as a Screen 13 page. So the excess memory provides for another page or two.

You can still do paging in Screen 13, but you have to either know how to hack the VGA or how to code it in assembler. The easiest way is to use library that'll do it for you. Some libraries also support higher resolutions that QB doesn't. A list of some good QB libraries can be found here.