Author Topic: Duplicate errors. Can't REDIM be used in a GOSUB?  (Read 3360 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Duplicate errors. Can't REDIM be used in a GOSUB?
« on: August 20, 2020, 11:55:56 am »
Making a game where a GOSUB sets level data.  I'm must be missing something obvious, but I'm getting a Duplicate Definition error when using this REDIM in a GOSUB routine.  Not sure why.  (I can already hear the..."Another reason to NOT use GOSUB") :) 

Here's a shortened code that brings up the IDE error notice.  Thanks for any help.

- Dav

Error version...
Code: QB64: [Select]
  1. GOSUB SetLevel
  2.  
  3. IF pdata$(1, 1) = "x" THEN END
  4.  
  5.  
  6.  
  7.  
  8. '=======
  9. SetLevel:
  10. '=======
  11.  
  12.  
  13. REDIM pdata$(10, 10)
  14.  
  15. FOR t = 1 TO 10
  16.     pdata$(t, t) = "v"
  17.  
  18. pdata$(1, 1) = "x"
  19.  
  20.  

If I move the REDIM stuff to the top from the GOSUB it will run...
Code: QB64: [Select]
  1. REDIM pdata$(10, 10)
  2.  
  3. FOR t = 1 TO 10
  4.     pdata$(t, t) = "v"
  5.  
  6. pdata$(1, 1) = "x"
  7.  
  8.  
  9. IF pdata$(1, 1) = "x" THEN END
  10.  
« Last Edit: August 20, 2020, 11:58:41 am by Dav »

Marked as best answer by Dav on August 20, 2020, 08:04:49 am

FellippeHeitor

  • Guest
Re: Duplicate errors. Can't REDIM be used in a GOSUB?
« Reply #1 on: August 20, 2020, 12:02:43 pm »
When you do this in line 3:

Code: QB64: [Select]
  1.  IF pdata$(1, 1) = "x" THEN END

You're forcing qb64 to create an undeclared array. If you have a REDIM line before that, you can REDIM as you like in the gosub block, because the variable will have been created as dynamic.

If you don't want to do that, just add this to the top:

Code: QB64: [Select]
  1. '$dynamic
« Last Edit: August 20, 2020, 12:04:20 pm by FellippeHeitor »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Duplicate errors. Can't REDIM be used in a GOSUB?
« Reply #2 on: August 20, 2020, 12:05:38 pm »
Thanks, Fellippe!  I WAS missing something obvious.

Appeciate it.

- Dav

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Duplicate errors. Can't REDIM be used in a GOSUB?
« Reply #3 on: August 22, 2020, 06:46:18 am »
this is an interesting issue...

it remembers me the issue and the bug that I found in Qbasic about DEF FN.

https://www.qb64.org/forum/index.php?topic=2524.msg117972#msg117972

I admit that I never have used DEF FN in Qbasic in my programs. Yes I know it theorically, but never I used it in a real program.
Nevertheless I tried to make a tool to convert old DEF FN of QB/QBasic into FUNCTION of QB64. But for my lackness of knowledge I thought DEF FN as a separate block of code not so different from FUNCTION or SUB. What a dumbness!! DEF FN is like a GOSUB RETURN block of code so it is a piece of the main module. To complete my tool I need to develope a parser to find the variables declared into DEF FN and the variables of the main that DEF FN uses because all these must be SHARED to not break the link of code!.

Why is it matter with your experience?
Reading your code I have thought that there is  no problem because in a flow of program from the high to the bottom and from left to the right of lines of source code  the line
Code: QB64: [Select]
  1.  IF pdata$(1, 1) = "x" THEN END
has been executed after  the declaration
Code: QB64: [Select]
  1. '=======
  2. SetLevel:
  3. '=======
  4.  
  5.  
  6. REDIM pdata$(10, 10)
and all these lines are in the main module... so why does the parser understand pdata$(1,1) ="x"  as a different array variable with declaration and initialization in the same line of code?
The answer is: it is possible that the lines of source code are managed and changed during the compiling process and so the line IF pdata$(1,1) = "x" THEN    is processed before the line REDIM pdata$(10,10).

This is the same that happens in QBasic with this code
Code: QB64: [Select]
  1. DEF FnQbasicBug (z%)
  2. DIM a%
  3. FnQbasicBug= z% * a%
  4.  
  5. a% = 10
  6. PRINT a%, FnQbasicBug(4)
  7.  
So we can affirm that this is another gift that QB64 has eredited from QB/Qbasic and it happens to duplicate the way of working of Qbasic.
Programming isn't difficult, only it's  consuming time and coffee