Author Topic: Problem to pass variables in a sub  (Read 4811 times)

0 Members and 1 Guest are viewing this topic.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Problem to pass variables in a sub
« on: May 15, 2021, 06:14:08 am »
Hi, I don't understand why is not possible something like that:

Code: QB64: [Select]
  1.  
  2. DIM SHARED c&(3), t$(3)
  3.  
  4. bianco& = _RGB32(255, 255, 255)
  5. giallo& = _RGB32(255, 255, 0)
  6. grigio& = _RGB32(127, 127, 127)
  7.  
  8. testo1$ = "a"
  9. testo2$ = "b"
  10. testo3$ = "c"
  11.  
  12. call prova(bianco&,testo1$,giallo&,testo2$,grigio&,testo3$)
  13.  
  14. sub prova(c&(1),t$(1),c&(2),t$(2), c&(3),t$(3))
  15. for i%=1 to 3
  16. color c&(i%)
  17. print t$(i%)
  18. next i%
  19.  

I have to do this instead:
Code: QB64: [Select]
  1.  
  2. bianco& = _RGB32(255, 255, 255)
  3. giallo& = _RGB32(255, 255, 0)
  4. grigio& = _RGB32(127, 127, 127)
  5.  
  6.  
  7. testo1$ = "a"
  8. testo2$ = "b"
  9. testo3$ = "c"
  10.  
  11. CALL prova(bianco&, testo1$, giallo&, testo2$, grigio&, testo3$)
  12.  
  13. SUB prova (c1&, t1$, c2&, t2$, c3&, t3$)
  14.  
  15.     COLOR c1&: PRINT t1$
  16.  
  17.     COLOR c2&: PRINT t2$
  18.  
  19.     COLOR c3&: PRINT t3$
  20.  

but I can't use the FOR loop and, however, colors variables don't pass to the sub...

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Problem to pass variables in a sub
« Reply #1 on: May 15, 2021, 06:42:12 am »
Why not do something like:

 
DIM SHARED c&(3), t$(3)

c&(1)= _RGB32(255, 255, 255)
c&(2) = _RGB32(255, 255, 0)
c&(3) = _RGB32(127, 127, 127)
 
t$(1) = "a"
t$(2) = "b"
t$(3) = "c"
 
call prova
 
sub prova
for i%=1 to 3
color c&(i%)
print t$(i%)
next i%
end sub
 


Why declare shared variables and then not use them?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: Problem to pass variables in a sub
« Reply #2 on: May 15, 2021, 07:24:57 am »
yes, but why colors variables don't pass?

And there is a way in order to load into an array colors variables using DATA?

Code: QB64: [Select]
  1. G& = _RGB32(0, 255, 0)
  2. giallo& = _RGB32(255, 255, 0)
  3.  
  4. DIM c&(8), t$(8)
  5.  
  6. Carica1:
  7. DATA 0,giallo&,giallo&,giallo&,g&,g&,0,0:
  8. Carica2:
  9. DATA "-","ore","ore:min","mc/sec","ore:min","N*tc","","":
  10.  
  11. RESTORE Carica1
  12. FOR i% = 1 TO 8
  13.     READ c&(i%)
  14. NEXT i%
  15.  
  16. RESTORE Carica2
  17. FOR i% = 1 TO 8
  18.     READ t$(i%)
  19. NEXT i%
  20.  

It doesn't work.
« Last Edit: May 15, 2021, 07:31:17 am by bartok »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Problem to pass variables in a sub
« Reply #3 on: May 15, 2021, 09:34:51 am »
I seem to remember having this issue once, and it was because I hadn’t set the screen mode first before using rgb32. I’m not at my pc today so I can’t test tif that is the reason here, but try that and see if is it.

- Dav

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Problem to pass variables in a sub
« Reply #4 on: May 15, 2021, 10:05:08 am »
Dav's correct
« Last Edit: May 15, 2021, 10:12:55 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Problem to pass variables in a sub
« Reply #5 on: May 15, 2021, 10:53:43 am »
Yes
Dav & Bplus are correct
I have fighted so much with _RGB _RGB32 _RGBA _RGBA32...and these are commandments:
1 create the screen
2 create the values of colors
3 use variable (preferred _unsigned long vs long) and not CONST
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Problem to pass variables in a sub
« Reply #6 on: May 15, 2021, 11:05:12 am »
@bartok
better this with global variable arrays passed as parameters to SUB
Code: QB64: [Select]
  1. Screen _NewImage(400, 400, 32)
  2. Dim cc&(3), tt$(3)
  3.  
  4. cc&(1) = _RGB32(255, 255, 255)
  5. cc&(2) = _RGB32(255, 255, 0)
  6. cc&(3) = _RGB32(127, 127, 127)
  7.  
  8. tt$(1) = "a"
  9. tt$(2) = "b"
  10. tt$(3) = "c"
  11.  
  12. Call prova(cc&(), tt$())
  13.  
  14. Sub prova (c&(), t$())
  15.     For i% = 1 To 3
  16.         Color c&(i%)
  17.         Print t$(i%)
  18.     Next i%
  19.  
  20.  

or this other one with global SHARED variables

Code: QB64: [Select]
  1. Screen _NewImage(400, 400, 32)
  2. Dim Shared cc&(3), tt$(3)
  3.  
  4. cc&(1) = _RGB32(255, 255, 255)
  5. cc&(2) = _RGB32(255, 255, 0)
  6. cc&(3) = _RGB32(127, 127, 127)
  7.  
  8. tt$(1) = "a"
  9. tt$(2) = "b"
  10. tt$(3) = "c"
  11.  
  12. Call prova
  13.  
  14. Sub prova
  15.     For i% = 1 To 3
  16.         Color cc&(i%)
  17.         Print tt$(i%)
  18.     Next i%
  19.  
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: Problem to pass variables in a sub
« Reply #7 on: May 15, 2021, 11:25:08 am »
Thank's you,
I solved in this way:

Code: QB64: [Select]
  1. [...]
  2.  
  3. CALL Tabella(0, "-", giallo&, "t/ta", giallo&, "q/qp", G&, "ore", G&, "mc/s*mm", 0, "", 0, "", 0, "")
  4.  
  5. [...]
  6. SUB Tabella (c1&, t1$, c2&, t2$, c3&, t3$, c4&, t4$, c5&, t5$, c6&, t6$, c7&, t7$, c8&, t8$)
  7.  
  8.     DIM c&(8), t$(8)
  9.  
  10.     SHARED FineColonna%()
  11.  
  12.     c&(1) = c1&: c&(2) = c2&: c&(3) = c3&: c&(4) = c4&: c&(5) = c5&: c&(6) = c6&: c&(7) = c7&: c&(8) = c8&: t$(1) = t1$: t$(2) = t2$: t$(3) = t3$: t$(4) = t4$: t$(5) = t5$: t$(6) = t6$: t$(7) = t7$: t$(8) = t8$
  13.     FOR i% = 1 TO 7
  14.         COLOR c&(i%): PRINT t$(i%);
  15.         SELECT CASE LEN(t$(i% + 1))
  16.             CASE IS <> 0
  17.                 IF t$(i%) = "-" THEN
  18.                     FineColonna%(i%) = POS(0) - 1 + 4: COLOR grigio&: LOCATE , FineColonna%(i%): PRINT "³";
  19.                 ELSE
  20.                     FineColonna%(i%) = POS(0) - (LEN(t$(i%)) + 1) + LarghezzaColonna%: COLOR grigio&: LOCATE , FineColonna%(i%): PRINT "³";
  21.                 END IF
  22.             CASE IS = 0
  23.                 FineColonna%(i%) = POS(0) - (LEN(t$(i%)) + 1) + LarghezzaColonna%
  24.                 EXIT SUB
  25.         END SELECT
  26.     NEXT i%
  27.     COLOR c8&: PRINT t8$;
  28.     FineColonna%(8) = POS(0) - (LEN(t8$) + 1) + LarghezzaColonna%
  29.  

I need to pass in the sub the arrays c&(8) and t$(8), but they have always diffent values when I call the sub. I tried to pass the array into the call, but considered it is not possibile, I have done as you have said, but I have put the array creation directly into the sub.

However, everything I do is on a _RGB32 screen, and I always fail to use DATA with colors.

An other question...


... at the and of the main code, instead to use _FREEIMAGE, why not use a simple CLEAR, that I think it will clear not only the images, but all variables and arrays?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Problem to pass variables in a sub
« Reply #8 on: May 15, 2021, 12:03:03 pm »
For 32bit color and DATA, the best method is to simply get used to using hex values.  &HAARRGGBB — alpha, red,green, blue

So full red is &HFFFF0000.  (FF alpha, FF red, 00 green, 00 blue)
full blue is &HFF0000FF
full green is &HFF00FF00

And so on, so your data looks like the following for 32-bit red, green, blue:

DATA &HFFFF0000, &HFF00FF00, &HFF0000FF

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Problem to pass variables in a sub
« Reply #9 on: May 15, 2021, 12:46:53 pm »
yes, but why colors variables don't pass?

And there is a way in order to load into an array colors variables using DATA?

Code: QB64: [Select]
  1. G& = _RGB32(0, 255, 0)
  2. giallo& = _RGB32(255, 255, 0)
  3.  
  4. DIM c&(8), t$(8)
  5.  
  6. Carica1:
  7. DATA 0,giallo&,giallo&,giallo&,g&,g&,0,0:
  8. Carica2:
  9. DATA "-","ore","ore:min","mc/sec","ore:min","N*tc","","":
  10.  
  11. RESTORE Carica1
  12. FOR i% = 1 TO 8
  13.     READ c&(i%)
  14. NEXT i%
  15.  
  16. RESTORE Carica2
  17. FOR i% = 1 TO 8
  18.     READ t$(i%)
  19. NEXT i%
  20.  

It doesn't work.

Variables can't be used in DATA
https://www.qb64.org/wiki/index.php/DATA

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Problem to pass variables in a sub
« Reply #10 on: May 15, 2021, 12:55:01 pm »
Quote
I need to pass in the sub the arrays c&(8) and t$(8), but they have always diffent values when I call the sub. I tried to pass the array into the call, but considered it is not possibile, I have done as you have said, but I have put the array creation directly into the sub.

Possible @TempodiBasic  did it already in post above your quote:
Code: QB64: [Select]
  1. Screen _NewImage(400, 400, 32)
  2. Dim cc&(3), tt$(3)
  3.  
  4. cc&(1) = _RGB32(255, 255, 255)
  5. cc&(2) = _RGB32(255, 255, 0)
  6. cc&(3) = _RGB32(127, 127, 127)
  7.  
  8. tt$(1) = "a"
  9. tt$(2) = "b"
  10. tt$(3) = "c"
  11.  
  12. Call prova(cc&(), tt$())
  13.  
  14. Sub prova (c&(), t$())  ' <<<<<<<<<<<<<<<<<<<<<<<<<<  pass arrays to sub
  15.     For i% = 1 To 3
  16.         Color c&(i%)
  17.         Print t$(i%)
  18.     Next i%
  19.  
  20.  
« Last Edit: May 15, 2021, 01:03:21 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
RE: CLEAR
« Reply #11 on: May 15, 2021, 01:00:57 pm »
Quote
An other question...


... at the and of the main code, instead to use _FREEIMAGE, why not use a simple CLEAR, that I think it will clear not only the images, but all variables and arrays?

Image handles not included in items for CLEAR:
https://www.qb64.org/wiki/CLEAR

The Image handles give access to blocks of memory for images, you might clear the variable handle but not the block of memory.
« Last Edit: May 15, 2021, 01:07:42 pm by bplus »

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: Problem to pass variables in a sub
« Reply #12 on: May 16, 2021, 04:18:06 am »
For 32bit color and DATA, the best method is to simply get used to using hex values.  &HAARRGGBB — alpha, red,green, blue

So full red is &HFFFF0000.  (FF alpha, FF red, 00 green, 00 blue)
full blue is &HFF0000FF
full green is &HFF00FF00

And so on, so your data looks like the following for 32-bit red, green, blue:

DATA &HFFFF0000, &HFF00FF00, &HFF0000FF

ah! interesting! Thank'you.


Image handles not included in items for CLEAR:
https://www.qb64.org/wiki/CLEAR

The Image handles give access to blocks of memory for images, you might clear the variable handle but not the block of memory.

ok, I have understood. Thank's.

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Problem to pass variables in a sub
« Reply #13 on: May 16, 2021, 04:29:25 am »


My way in theese cases:

use a well-structured variable in _clipboard$

The sub program read _clipboard$ and decode / reconstitute it's elements...
Why not yes ?

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Problem to pass variables in a sub
« Reply #14 on: May 16, 2021, 11:39:32 am »
Not that it directly applies to your problem, but variables containing colors
should be defined _UNSIGNED LONG. 
It works better if you plug it in.