Menu

Any future plans for Byref style parameter for subs ?

ToniG
2022-05-28
2022-05-29
  • ToniG

    ToniG - 2022-05-28

    I have a sub that is part of an include file & I want to process a string from the main code, so the sub won't know in advance the string variable name, unless it can be passed or set somehow from main.
    The string might be very long so not really practical to pass the variable itself.
    It would be possible to specify the String name the main code has to use but would be preferable to let the programmer choose.
    or pass a string of the VarName but I don't think think this can be applied to a variable name in the sub. ( I tried)

     
    • Anobium

      Anobium - 2022-05-28

      Try this.

      Pass the string from main to your sub. In the sub use ( in localarray() ). So, you treat the the string as an array in the sub. This should work up to 254 string length.

       
  • ToniG

    ToniG - 2022-05-29

    I was thinking a false assumption...
    Passing a string to a Sub already behaves as I want, just needed to get the structure right.
    RAM is only allocated for the source string (not duplicated by the sub as I first thought)

    Here's the quick test code

    #OPTION explicit
    #chip 12F683,8
    #config Osc = Int
    
    Dim StringVar as String * 36
    StringVar =  "0123456789abcdefghijklmnopqrstuvwxyz"
    
    Main:
      ExtractASCII_2(StringVar)
    ';    ExtractASCII_2("0123456789abcdefghijklmnopqrstuvwxyz")    ' <-same Mem. table
    
    Goto Main
    Return  ' <- Just 4 IDE
    
    Sub ExtractASCII_2 (SubStr as string)
    Dim i, StrPos, StrChr
          StrPos = SubStr(0)
        For i = 1 to SubStr(0)
          StrChr = SubStr(StrPos)
          '; process  StrChr...
          StrPos--
        Next
    End Sub
    
     
  • Anobium

    Anobium - 2022-05-29

    Looks good to me. But, if you treat the string like an array it will be a lot faster.

    Recommend not to use single letter variables as some MCUs have one letter bits.

     
  • ToniG

    ToniG - 2022-05-29

    " if you treat the string like an array it will be a lot faster"

    I thought I was here...
    SubStr(0)
    SubStr(StrPos)

    Do you mean reference the string as an array ?
    I can only get (SubStr as string) to work
    (SubStr as Array) Does not compile.

     
    • Anobium

      Anobium - 2022-05-29

      This will work, but, it is any faster? It may not make any difference.

      Note: I am using _Substr as there is function called Substr

         Dim StringVar as String * 36
          StringVar =  "0123456789abcdefghijklmnopqrstuvwxyz"
      
      
          ExtractASCII_2(StringVar)
          ';    ExtractASCII_2("0123456789abcdefghijklmnopqrstuvwxyz")    ' <-same Mem. table 
      
          End
      
          Sub ExtractASCII_2 ( in _SubStr() )
          Dim StrPos, StrChr
      
              For StrPos = 1 to _SubStr(0)
                  StrChr = _SubStr(StrPos)
              Next
          End Sub
      
       

      Last edit: Anobium 2022-05-29
  • ToniG

    ToniG - 2022-05-29

    Thanks .

    Looking in Help under
    ... Subroutines, Using subroutines:
    ... Arrays:

    I don't see any mention of empty brackets () as reference to an Array.
    Search engine doesn't like () but searching the PDF help shows examples.

    cheers


    Sub  Get_Chr( in Str1() )
    
      ----- or  ----     
          Get_Chr "12345"
    
    Sub  Get_Chr( in Str1() ) 
            StrChr = Str1(3)      '  StrChr = 51
    End Sub        
    
     

Log in to post a comment.