Passing arrays to subs

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
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Passing arrays to subs

Post by Theophage » Sat Oct 13, 2007 9:11 am

Okay, I tried searching google for "passing arrays subs quickbasic" and I couldn't find what I needed. Even the qb45 help couldn't help me.

I'm writing a game which I've recently decided to to all in text graphics (chr$(219)s and chr$(177)s) so I've had to write my own text GET and PUT routines. Here is my PUT routine:

Code: Select all

SUB TextSprite (xpos, ypos, array(), index)
  ctr = index + 2
  FOR v = ypos to ypos + array(index + 1) - 1
    FOR h = xpos to xpos + array(index) - 1
      colr = array(ctr)
      IF colr > 0 AND h > -1 AND v > -1 AND h < 80 AND v < 50 THEN
        IF colr < 128 THEN
          POKE h * 2 + v * 160, 219
          POKE h * 2 + v * 160 + 1, colr
        ELSE
          POKE h * 2 + v * 160, 177
          POKE h * 2 + v * 160 + 1, colr - 128
        END IF
      END IF
      ctr = ctr + 1
    NEXT h
  NEXT v
END SUB
You will note that this routine requires that I pass the array which holds the sprite data into the SUB as well the index value of where in the array the particular sprite resides. When I use the above code, however, I get a type mismatch error.

So how do I (properly) pass an array into a SUB? Or, if it cannot be done, what can I do as a work around?
Last edited by Theophage on Sat Oct 13, 2007 9:41 am, edited 2 times in total.
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."

User avatar
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Post by Theophage » Sat Oct 13, 2007 9:26 am

Dammit, I should really play around a bit more before posting; I found my problem.

It seems that QB wanted to make my variables SINGLE by putting the "!" by their names in the DECLARE, but that wasn't in my SUB. In fact, in the program I defined the array to be an INTEGER. So what I did was put "AS INTEGER" in all three places (the DECLARE statement, my DIM statement, and in the SUB statement) and now it seems to work fine.
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac » Sat Oct 13, 2007 4:37 pm

[quote="Theophage"]Dammit, I should really play around a bit more before posting; I found my problem.

It seems that QB wanted to make my variables SINGLE by putting the "!" by their names in the DECLARE, but that wasn't in my SUB. In fact, in the program I defined the array to be an INTEGER. So what I did was put "AS INTEGER" in all three places (the DECLARE statement, my DIM statement, and in the SUB statement) and now it seems to work fine.[/quote]

Good thing you found it. Nobody would help you because you haven't even acknowledged help on your previous post.

Mac

User avatar
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Post by Theophage » Sat Oct 13, 2007 9:29 pm

To be honest, Mac, I hadn't touched that program since I already got help on it (and responded), so I hadn't even implemented the change you suggested on it afterward. Going over my plans for the game I'm (hoping to) write, I decided I would like to try it in the text mode. But I understand how it is to not get a response, so I just wrote one over in the thread. I do thatnk you for your help. :)
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."

User avatar
burger2227
Veteran
Posts: 2378
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Wed Oct 31, 2007 6:31 pm

SNARK!!!!!!!!!!

Post Reply