Author Topic: option explicit  (Read 2806 times)

0 Members and 1 Guest are viewing this topic.

Offline Ivan

  • Newbie
  • Posts: 17
  • Mild dyslexia
    • View Profile
option explicit
« on: May 09, 2021, 05:08:04 am »
Hi,

Although I'm in the middle in a program, I can't resist trying out features in qb64. I'm very pleased to discover, that I don't have to use percent or dollar signs. These signs gives me lot of headache in my current basic, because I overlook them.

And indentions in subs are great. My current basic also lack that.

Option explicit could for my behalf be even more strict. I would welcome forced variable name after next and after end sub. The latter is not allowed.

I have to figure out how I change the error color, when typing something wrong. The colors contrast is hard for me to distinguish between.

String argument to sub..?

Regards,

Ivan

Code: QB64: [Select]
  1. dim start_int, end_int, counter, step_val as integer
  2. dim abc as string * 2
  3.  
  4. start_int = 2
  5. end_int   = 5
  6. counter   = 1
  7. step_val  = 1
  8. abc       = "de"
  9.  
  10. call test(counter, start_int, end_int, abc)
  11.  
  12. sub test(num1, num2, num3, text1)
  13.   for num1 = num2 to num3
  14.     'print num1;","; abc
  15.     print num1
  16.   next num1
  17. end sub test
  18.  

« Last Edit: May 09, 2021, 05:47:20 am by Ivan »
Started with BBCBASIC in 1984 and UniComal. Some expirence wth c++ and database. For the last two years I had coded almost every day mostly in Basic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: option explicit
« Reply #1 on: May 09, 2021, 12:03:25 pm »
You can't change red line for IDE error spotting, but you can change background IDE color?

Some fixes and comments
Code: QB64: [Select]
  1. 'Dim start_int, end_int, counter, step_val As Integer ' <<<<<<<<<<<<<< do you want all these Integers?
  2. Dim As Integer start_int, end_int, counter, step_val
  3. Dim abc As String * 2 ' fixed string at 2 chars
  4.  
  5. start_int = 2
  6. end_int = 5
  7. counter = 1
  8. step_val = 1
  9. abc = "de"
  10.  
  11. 'call test(counter, start_int, end_int, abc)    ' <<<< don't need call with ()
  12. test counter, start_int, end_int, abc ' <<< without call and without () around arguments
  13.  
  14. ' just to show you something important when use variables of proper type they could be changed by sub
  15. Print "counter now at"; counter
  16.  
  17.  
  18. Sub test (num1 As Integer, num2 As Integer, num3 As Integer, text1 As String) ' <<< looking for a types , best not to depend on default = Single usually
  19.     For num1 = num2 To num3
  20.         'print num1;","; abc
  21.         Print num1
  22.     Next num1
  23.     'end sub test     ' <<< no test
  24.  
  25.  
  26.  

Oh man! @Ivan I just noticed all your keywords are lower case and your = signs are lined up like ducks in a row! :O
« Last Edit: May 09, 2021, 02:30:44 pm by bplus »

Offline Ivan

  • Newbie
  • Posts: 17
  • Mild dyslexia
    • View Profile
Re: option explicit
« Reply #2 on: May 10, 2021, 04:26:06 am »
No problem - I'm trying to minimize my misreading and this lining up is helping me. I love ducklings... :o) And thanks for helping.

I have not figured out how to concantenate text variables, increment/decrement in short notation and using global variables.

Code: QB64: [Select]
  1. ' trying qb64 v2
  2. ' Status line says in green: "OK (1 warning - click here or Ctrl to view)
  3. ' But I can't find the error...
  4.  
  5. dim number as integer
  6. dim abc as string * 8
  7. dim as integer num1                                                  ' if not initialized then default value = 0
  8. dim as single num3                                                   ' how do I declare globel variables?
  9.  
  10. number = 2
  11. abc    = "de"
  12.  
  13. print "number before call: "; number
  14. print "abc before call   : "; abc
  15. call test1(number, abc)
  16. print "number after call : "; number
  17. print "abc after call    : "; abc
  18.  
  19.  
  20. print "num1 before sub: "; num1
  21. print "abc before sub : "; abc
  22. test2 num1, abc
  23. print "num1 after sub : "; num1
  24. print "abc after sub  : "; abc
  25.  
  26.  
  27. sub test1 (num, text as string)                                      ' num is regarded as local aka call by value
  28.   print "1. inside test: num = "; num
  29.   num = num + 1                                                      ' how do I increment? (I'm used to: += add one to a variable)
  30.   print "2. inside test: num = "; num
  31.   text = text + "hj"                                                 ' used to: text += "hj"
  32.   print "text: "; text
  33. end sub 'test
  34.  
  35. sub test2 (num2 as integer, text2 as string)                         ' call by reference
  36.   num2  = num2 + 5
  37.   print "a. text2: "; text2
  38.   text2 = text2 + "fg"
  39.   print "b. text2: "; text2                                          ' print de     fg     I exspected defg


Started with BBCBASIC in 1984 and UniComal. Some expirence wth c++ and database. For the last two years I had coded almost every day mostly in Basic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: option explicit
« Reply #3 on: May 10, 2021, 12:35:51 pm »
Quote
I have not figured out how to concantenate text variables, increment/decrement in short notation and using global variables.

Concatenate
text3$ = text1$ + text2$ ' "add" as many as you want

Global Variables use SHARED with DIM or REDIM:
DIM (or REDIM for Dynamic Arrays) SHARED x$, arr$(1 to 60), ...

STATIC in a SUB will preserve value between calls to that SUB (I consider that a little more advanced).

Quote
increment/decrement in short notation
Not sure what you mean
x += increment   ' <<< does not work in QB64

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: option explicit
« Reply #4 on: May 10, 2021, 12:38:46 pm »
Quote
' Status line says in green: "OK (1 warning - click here or Ctrl to view)
' But I can't find the error...

It also says on line 9 num3 and means nothing wrong you just haven't used that variable yet (it's pretty smart heh? LOL)

Code: QB64: [Select]
  1. Dim Shared As Single num3 ' <<<<  declare global variables
  2.  
« Last Edit: May 10, 2021, 12:41:05 pm by bplus »