Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Raptor88

Pages: [1] 2
1
Hi Tempodi,

Thanks for taking the time to explain.  All makes sense. 

Am learning as I go,
Raptor88

2
Personally, I prefer to make SUBs with the variables explicitly defined, such as in the original.

The reason?

I prefer modular coding techniques and want to make my subs and functions as simple as possible to move from one program to another.

Let’s say I have a program like this:

DEFINT A-Z
..... stuff
SUB Whatever (x, y)
.....more stuff
END SUB

Now, x and y are integers and everything runs just fine...

BUT....

What if I want to reuse that sub in other code that starts with _DEFINE A-Z AS _FLOAT?  Will it still function and work as intended?  Will it generate rounding errors over time?  Can I now get edge-case exceptions where 3.2 + 4.4 = 7.6, which might round to 8??

By explicitly defining SUB Whatever (x AS INTEGER, y AS INTEGER), it can be ripped out and reused in another program, with fewer concerns and issues.

Explicitly defining SUB variables for reuse of SUBs in another program is something I had not thought of.  Thanks for this info.
Raptor88

3
Hi guys

thanks to all you to read this post...
I have tried to emulate an ASCII screen in 32bit  to create a light moving with arrows...

here is
Code: QB64: [Select]
  1. DIM SHARED A AS LONG, whites(1 TO 50) AS LONG, Black AS LONG
  2. DIM min AS INTEGER, max AS INTEGER, up AS INTEGER, down AS INTEGER
  3.  
  4. Setup
  5. CalculateRows
  6.  
  7. col = 1
  8. row = 10
  9. min = 1
  10. max = 44
  11. up = 1
  12. down = 23
  13.  
  14.     IF _KEYDOWN(19200) THEN
  15.         LOCATE 27, 1: COLOR whites(1): PRINT "Left"
  16.         IF col > min THEN col = col - 1
  17.     END IF
  18.     IF _KEYDOWN(19712) THEN
  19.         LOCATE 27, 1: COLOR whites(1): PRINT "Right"
  20.         IF col < max THEN col = col + 1
  21.     END IF
  22.     IF _KEYDOWN(18432) THEN
  23.         LOCATE 27, 1: COLOR whites(1): PRINT "Up"
  24.         IF row > up THEN row = row - 1
  25.     END IF
  26.     IF _KEYDOWN(20480) THEN
  27.         LOCATE 27, 1: COLOR whites(1): PRINT "Down"
  28.         IF row < down THEN row = row + 1
  29.     END IF
  30.  
  31.     MoveLight row, col, 10, 1, 1
  32.     LOCATE 26, 1: COLOR _RGB(255, 255, 55), Black: PRINT " press arrows to move light, space to quit ";
  33.     _LIMIT 15
  34.     CLS
  35.  
  36. SUB MoveLight (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  37.     outputs row, col, min, max, -steps
  38.     outputs row, min + col, max, min, steps
  39.     outputs row + 1, col, min, max, -steps
  40.     outputs row + 1, min + col, max, min, steps
  41.     outputs row + 2, col, min, max, -steps
  42.     outputs row + 2, min + col, max, min, steps
  43.  
  44. SUB outputs (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  45.     LOCATE row, col
  46.     FOR r = min TO max STEP steps
  47.         COLOR Black, whites(r): PRINT "0";
  48.     NEXT
  49.  
  50. SUB Setup
  51.     A = _NEWIMAGE(500, 500, 32)
  52.     IF A THEN SCREEN A ELSE PRINT "Error create SCREEN"
  53.     Black = _RGB(0, 0, 0)
  54.     whites(1) = _RGB(255, 255, 255)
  55.     r = 255
  56.     b = 255
  57.     g = 255
  58.     counter = 1
  59.     FOR counter = 1 TO 10
  60.         r = r - INT(255 / 10)
  61.         b = b - INT(255 / 10)
  62.         g = g - INT(255 / 10)
  63.         whites(counter) = _RGB(r, g, b)
  64.     NEXT counter
  65.  
  66. SUB CalculateRows
  67.     LOCATE 1, 1: COLOR _RGB(0, 127, 127)
  68.     FOR x = 1 TO 80 STEP 1
  69.         IF x > -1 THEN PRINT LTRIM$(STR$(x MOD 10));
  70.     NEXT x
  71.     SLEEP 3
  72.  
  73.  

Hi Tempodi,

I just started using QB64.  Regarding your SUB version, how about using this as the first line in the code:
DEFINT A-Z

I believe that would automatically define all variables as integers unless otherwise DIM'ed as longs.

Then the line:
SUB MoveLight (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)

could be simplified as:
SUB MoveLight (row, col, min, max, steps)

It would also negate the need for the lines:
DIM row AS INTEGER, col AS INTEGER
DIM min AS INTEGER, max AS INTEGER, up AS INTEGER, down AS INTEGER
and just use:
col = 1
row = 10
min = 1
max = 44
up = 1
down = 23

I'm new to QB64 so I'm not sure if doing the above has any cons when most variables are integers.
PS:  I'm learning from your code as a clean example of how to use SUBS.

4
QB64 Discussion / Re: GOSUB VS SUB
« on: April 20, 2019, 09:44:47 pm »
If you use DIM SHARED from within a SUB or FUNCTION, it makes those particular variables global only within that SUB or FUNCTION.  Using it within the SUB or FUNCTION makes cleaner code, as you know at first glance what is being used globally.

Ah, helpful to know info about SHARED within a SUB or FUNCTION.
Thanks,
Raptor88

5
QB64 Discussion / Re: GOSUB VS SUB
« on: April 20, 2019, 09:02:03 pm »
Hi Raptor88

I agree with you that each one takes own method to code.

I don't agree with this because with GOSUB all your variables are already in the main and are Global!
So with or without SHARED you use global variables.
A different kind of issue is that rised up by Fellippe
you can make a GOSUB inside a SUB or FUNCTION,

 but in this case the variables used in the GOSUB are those Global with SHARED into the main and those local of the SUB where GOSUB is.

Guess global has different levels of being "global".
Without SHARED, then the variables are not seen within SUBs.

Thanks for sharing ... no pun intended ;)

6
QB64 Discussion / Re: GOSUB VS SUB
« on: April 20, 2019, 04:51:06 pm »
Hi bplus,

Yes, both of the examples you posted are solutions.
For solution1, I didn't want to have to type 6 arguments for each CALL.
For solution2, I thought not making all of my variables global using SHARED was the way to go.
I'm using CALL for now as a novice, to make it obvious to me it is a call to a SUB.  I may use your method later for less typing.

Thanks for taking the time to help.  I do appreciate it,
Raptor88

Hi Raptor88,

A simple fix to your print SUB, giving it a parameter to pass it an argument to PRINT:
Code: QB64: [Select]
  1. FOR x = 1 TO 10
  2.     GOSUB PrintStats
  3.     'CALL PrintStats2(x) '<<< yuck to CALL and ()'s
  4.    PrintStats2 x
  5.  
  6. '========== SUBROUTINES ==========
  7. PrintStats:
  8. PRINT "From GOSUB PrintStats: The value of x is: "; x
  9.  
  10. SUB PrintStats2 (printThisNumber) '<<< Fixed with a parameter to pass PRINT and argument to PRINT
  11.     PRINT "From PrintStats2: The value of x is: "; printThisNumber
  12.  
  13.  

PS I don't usually use CALL so I don't have to put arguments inside ()'s

Everything inside a SUB is completely ignorant of variables and values from another part of program. It only knows what you pass it from outside or what is DIM SHARED in main code.

So another way to do this:
Code: QB64: [Select]
  1. DIM SHARED x AS INTEGER '<<< if you share x, you don't need to pass PrintStats2 an argument
  2. FOR x = 1 TO 10
  3.     GOSUB PrintStats
  4.     PrintStats2
  5.  
  6. '========== SUBROUTINES ==========
  7. PrintStats:
  8. PRINT "From GOSUB PrintStats: The value of x is: "; x
  9.  
  10. SUB PrintStats2
  11.     PRINT "From PrintStats2: The value of x is: "; x
  12.  
  13.  

7
QB64 Discussion / Re: GOSUB VS SUB
« on: April 20, 2019, 04:58:32 am »
While programming, I discovered a reason why GOSUB should be used.

Code: QB64: [Select]
  1. x = 100
  2. GOSUB PrintStats
  3. CALL PrintStats2
  4. '========== SUBROUTINES ==========
  5. PrintStats:
  6. PRINT "The value of x is: "; x
  7.  
  8. SUB PrintStats2
  9.     PRINT "The value of x is: "; x

The print was:
The value of x is: 100  <== GOSUB printed the value
The value of x is: 0   <=== SUB did not print the value

I wanted a subroutine to print the values of 6 variables (statistics) at various points for troubleshooting.
Using SUB, the values were always zero, since I did not pass all of the variables to the SUB as arguments.
Using GOSUB, all of the values were printed without having to pass all of the variables as arguments.

It would be too unweildy to have to type 6 (or more) variables as arguments every time I inserted a CALL to the SUB.

So GOSUB should be used to print the contents of variables that have not been declared as SHARED.

Raptor88

8
In QB64, is there a shortcut way to add something to itself?

For example:
MyRunningTotal = MyRunningTotal +1
-or-
MyRunningTotal = MyRunningTotal + 22

A shortcut to negate having to retype MyRunningTotal again on the right side of the equation?

Fellippe, Thanks for confiming that there is no built-in way to do want I asked.
bplus, Thanks for your efforts in creating work-around methods.

Since there is no built-in way in QB64 to do what I want, I created a script for the "AutoHotKey" program to Add, Subtract, Multiply and Divide a value to itself.
Here's my script:

;===== QB64 "Add, Subtract, Multiply, Divide" value to original variable =====
;.... Type variable name, then press (Ctrl +) to fill in the rest of the equation.
#IfWinActive ahk_class FREEGLUT
^=::         ; (Ctrl +) writes "x = x + "                     
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{+}{space}
return
^-::         ; (Ctrl -) writes "x = x - "
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{-}{space}
return
^8::         ;(Ctrl *) writes "x = x * "
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{*}{space}
return
^/::         ;(Ctrl /) writes "x = x / "
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{/}{space}
return
#IfWinActive

The "#IfWinActive ahk_class FREEGLUT" line allows this script to only run in QB64.
Type "MyRunningTotal" and then press the (Ctrl +) keys to fill in the rest of the equation as:
"MyRunningTotal = MyRunningTotal + "

Hope this helps others,
Raptor88

9
QB64 Discussion / Is there a shortcut way to add something to itself?
« on: April 18, 2019, 07:15:15 pm »
In QB64, is there a shortcut way to add something to itself?

For example:
MyRunningTotal = MyRunningTotal +1
-or-
MyRunningTotal = MyRunningTotal + 22

A shortcut to negate having to retype MyRunningTotal again on the right side of the equation?

In C, I believe to increment by 1, the following can be used:
MyRunningTotal++

Something similar in QB64?

10
QB64 Discussion / Re: GOSUB VS SUB
« on: April 17, 2019, 09:59:44 pm »
To further add to what I said:
.... snip to save bandwidth ....

Interesting read.
Thanks,
Raptor88

11
QB64 Discussion / Re: GOSUB VS SUB
« on: April 17, 2019, 09:57:59 pm »
Hi Raptor88,

Here is an example of a recursive sub. It calls itself over and over again until the job is done.

IMHO it is the most beautiful and elegant kind of code you can write. I hope this fractal inspires:
.... snip to save bandwidth ....

Nice screensaver.  As previously mentioned, recursive subs are beyond my scope for now but it's informative to see a working example.
Thanks,
Raptor88

12
QB64 Discussion / Re: GOSUB VS SUB
« on: April 17, 2019, 12:27:36 am »
Yes this is true about the variable arguments passed to the sub but all the variables created inside the sub are erased when the sub finishes so if you use an i in the sub, it won't effect the i in the main code as it would with GOSUB.

Ahhh, now the light bulb has turned on.  Thanks for taking the time to explain this valuable info.
Raptor88

13
QB64 Discussion / Re: GOSUB VS SUB
« on: April 16, 2019, 10:13:35 pm »
Just to add:

GOSUB + RETURN was the original BASIC method for subroutines, such as what you would see in GW-BASIC.  SUB + END SUB was a new feature added to QBASIC, and supported by a few other BASIC variants.

I personally recommend using SUB + END SUB, as it can do more, and do it simpler.  As far as I know, GOSUB labels do not show up under F2 to list subs/functions, which is a really great feature of SUB + END SUB.

Thank you for the history lesson.  I didn't know any of what you just explained.  I will use SUB + END SUB as you recommend since that is the more modern method.
Raptor88

14
QB64 Discussion / Re: GOSUB VS SUB
« on: April 16, 2019, 10:10:33 pm »
You should use SUB ... END SUB always for
1. recursive subs
2. variable isolation from main code and other subs
3. passing arguments
1. recursive subs
.... beyond my scope for now, but good to know.

2. variable isolation from main code and other subs
.... This I don't understand.  When I pass a variable as an argument, changes made to the variable within the sub changes the original variable.  I read that this is by design.

3. passing arguments
.... Yes, I haven't seen how to pass arguments using GOSUB, so SUB would be the way to do that.  Good point.

Thanks for the help,
Raptor88

15
QB64 Discussion / Re: GOSUB VS SUB
« on: April 16, 2019, 10:01:58 pm »
In summary, it's up to you.
Thanks for your input.
Raptor88

Pages: [1] 2