Possible to store an array in a sub?
- Raspberrypicker
- Veteran
- Posts: 55
- Joined: Thu Aug 02, 2007 4:54 pm
- Location: Florida
Possible to store an array in a sub?
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.
Thanks in advance...even if there is no solution my program will be fine.
Fruit Pickin'
-
- Veteran
- Posts: 399
- Joined: Wed Mar 02, 2005 9:01 pm
- Location: Nashville, Tennessee
- Contact:
You have to DIMension the array before you use it. And then you have to pass it to the SUB. ie:
See if you can make heads or tails of that, and then post your results
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
- Raspberrypicker
- Veteran
- Posts: 55
- Joined: Thu Aug 02, 2007 4:54 pm
- Location: Florida
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.
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.
Fruit Pickin'
I didn't noticed that Patz called the sub with the array , 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;
or like this
I like the later way rather than the first.
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
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
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.
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.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.
- Raspberrypicker
- Veteran
- Posts: 55
- Joined: Thu Aug 02, 2007 4:54 pm
- Location: Florida
- Raspberrypicker
- Veteran
- Posts: 55
- Joined: Thu Aug 02, 2007 4:54 pm
- Location: Florida
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?
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'
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.
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.