Shop Learn P1 Docs P2 Docs
Cannot use sub arguments in loops while option implicit — Parallax Forums

Cannot use sub arguments in loops while option implicit

pik33pik33 Posts: 1,732
edited 2022-01-13 13:01 in BASIC (for Propeller)
sub stopmotors(fl,fr,t)

' Stop motor from high speed to 0 in time t*10ms

dl=fl/t
dr=fr/t
for i= 0 to t
  fl=fl-dl : fr=fr-dr
  if fr<0 then fr=0
  if fl<0 then fl=0
  waitms 10: wypin(l_motor_pwm,fl) : wypin(r_motor_pwm,fr)   
next i  
pinlo(r_motor_enable) : pinlo(r_motor_break) : pinlo(l_motor_enable) :pinlo(l_motor_break) 
end sub

This generates warnings:

C:/Programowanie/ultibo-robot/01 obrum/Propeller/robuv_obrum023.bas:149: warning: definition of fl hides a member variable
C:/Programowanie/ultibo-robot/01 obrum/Propeller/robuv_obrum023.bas:156: note: previous definition of fl is here
C:/Programowanie/ultibo-robot/01 obrum/Propeller/robuv_obrum023.bas:149: warning: definition of fr hides a member variable
C:/Programowanie/ultibo-robot/01 obrum/Propeller/robuv_obrum023.bas:156: note: previous definition of fr is here

as if the compiler treats the loop as something other than the sub and don't tries to redeclare fl and fr.
Line 149 is

sub stopmotors(fl,fr,t)

What can I do? Option explicit and dim all variables (..... :( ...... ) ? Or is there any other way to do this?


Edit: removed option implicit while the program is still small, dimed all vars, now the compiler doesn't complain, but still.. it should not redeclare inner loop variables when they are declared in the function (if I declared these as var at the start of the function, the compiler still tried to redeclare them inside the loop.)

Comments

  • ersmithersmith Posts: 5,403

    @pik33 : That warning is bogus, and I'll try to fix it. But the generated code is correct, that is, "fl" and "fr" really are treated as function parameters.

    Thanks for the bug report,

  • pik33pik33 Posts: 1,732
    edited 2022-02-04 14:35

    Another "option implicit" problem

    if I have

    statusline$="something"
    sl=len(statusline$) 
    

    and then try to use the "sl" inside a sub, it is redeclared as a local variable inside this sub so it doesn't work.

    I tried to dim this variable to make it global, but then after this:

    dim sl as ulong
    statusline$="(the long line of text)"
    sl=len(statusline$) 
    

    I got

    error: Re-defining member sl

    The simplest test code to reproduce:

    This doesn't compile:

    option implicit
    dim sl as ulong
    statusline$="something"
    sl=len(statusline$) 
    

    This compiles:

    option explicit
    dim statusline$ as string
    dim sl as ulong
    statusline$="something"
    sl=len(statusline$) 
    

    This also compiles:

    option implicit
    dim sl(0) as ulong
    statusline$="something"
    sl(0)=len(statusline$) 
    

    and I can even see this sl(0) inside a sub

  • ersmithersmith Posts: 5,403

    It looks like "option implicit" is causing more trouble than it is worth. I think I'll remove it soon, but for now just avoid it and use "option explicit".

  • pik33pik33 Posts: 1,732
    edited 2022-02-05 19:28

    Don't remove this... This is Basic... Classic code will no more work...

    Edit: I can't assign to variable but I can read it.
    Instead of test=1, which generates error, I can lpoke(addr(test),1) which works. I can then print test, which prints what I poked there

Sign In or Register to comment.