Possible to store an array in a sub?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Possible to store an array in a sub?

Post by Raspberrypicker » Sat Aug 25, 2007 8:37 pm

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.
Fruit Pickin'

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Sat Aug 25, 2007 9:48 pm

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 ;)

User avatar
BadMrBox
Veteran
Posts: 86
Joined: Tue Feb 28, 2006 12:19 pm

Post by BadMrBox » Sat Aug 25, 2007 11:51 pm

The array need's to be Dim Shared if it's gonna be noticed in a sub or function.

User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Post by Raspberrypicker » Sun Aug 26, 2007 4:02 pm

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
Fruit Pickin'

User avatar
BadMrBox
Veteran
Posts: 86
Joined: Tue Feb 28, 2006 12:19 pm

Post by BadMrBox » Sun Aug 26, 2007 7:23 pm

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.

User avatar
k7
Coder
Posts: 41
Joined: Wed Aug 01, 2007 7:38 am
Location: Tasmania, Australia
Contact:

Post by k7 » Mon Aug 27, 2007 1:29 am

The latter way is incorrect. Shouldn't the loops be from 0 to 5?

Dr_D
Veteran
Posts: 58
Joined: Fri Jun 17, 2005 4:47 pm
Contact:

Post by Dr_D » Mon Aug 27, 2007 1:34 am

Perhaps lbound to ubound?
The Dr. is INsane!!!

ThemePark
Coder
Posts: 17
Joined: Wed Aug 22, 2007 6:37 pm

Post by ThemePark » Mon Aug 27, 2007 8:43 am

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.

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe » Mon Aug 27, 2007 9:20 am

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. :)

User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Post by Raspberrypicker » Mon Aug 27, 2007 7:40 pm

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.
Fruit Pickin'

User avatar
BadMrBox
Veteran
Posts: 86
Joined: Tue Feb 28, 2006 12:19 pm

Post by BadMrBox » Tue Aug 28, 2007 3:39 pm

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.

User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Post by Raspberrypicker » Tue Aug 28, 2007 6:18 pm

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?
Fruit Pickin'

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe » Tue Aug 28, 2007 6:34 pm

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.

Post Reply