Results 1 to 5 of 5

Thread: Problem passing strings to function

  1. #1

    Problem passing strings to function

    Hello Forum,
    Could someone please explain why this program prints "Bacon, Bacon" instead of "Bacon, Tomato"?

    '---Load Console Module
    Uses "Console"
    
    Dim s(2) as String
    Dim sResult as String
    
    function  sTest( ByRef s1 as String, ByRef s2 As String) As String
      Function = s1 + ", " + s2
    End Function
    
    s(1) = "Bacon"
    s(2) = "Tomato"
    
    sResult = sTest( s(1), s(2))
    
    printl sResult
    
    WaitKey
    
    If string array elements are not passed to the function, then the correct output "Bacon, Tomato" is produced.

    '---Load Console Module
    Uses "Console"
    
    Dim t1 As String
    Dim t2 as String
    
    Dim sResult as String
    
    function  sTest( ByRef s1 as String, ByRef s2 As String) As String
      Function = s1 + ", " + s2
    End Function
    
    t1 = "Bacon"
    t2 = "Tomato"
    
    sResult = sTest( t1, t2 )
    printl sResult
    
    '---Wait for a key press
    WaitKey
    

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,814
    Rep Power
    10
    Hi Eric,

    thanks for reporting this. It is a bug
    I will have a look and fix for the next thinBasic version.

    Problem is generated by passing BYREF a string array element.
    Parser is not considering the element index but always pointing to the very first element of the array s().

    A work around is to define BYVAL the string parameters.
    In this case it will work because parser will first create a copy of the string expressions s(1) and s(2) and then pass them to the function parameters.

    Another work around is to pass BYREF the full array to the function and use array index inside the function.

    Will report when solved.

    Ciao
    Eros

    '---Load Console Module
    Uses "Console"
     
    Dim s(2) as String
    Dim sResult as String
    
    function  sTest_ByRef(byref s1 as String, byref s2 As String) As String
      Function = s1 + ", " + s2
    End Function
    
    function  sTest_ByVal(byval s1 as String, byval s2 As String) As String
      Function = s1 + ", " + s2
    End Function
    
    function  sTest_Array(byref StringArray() As String) As String
      Function = StringArray(1) + ", " + StringArray(2)
    End Function
     
    s(1) = "Bacon"
    s(2) = "Tomato"
     
    sResult = sTest_ByRef( s(1), s(2))
    printl "sTest_ByRef ", sResult
    
    sResult = sTest_ByVal( s(1), s(2))
    printl "sTest_ByVal ", sResult
    
    sResult = sTest_Array(s)
    printl "sTest_Array ", sResult
     
    WaitKey
    
    Last edited by ErosOlmi; 05-06-2024 at 06:57.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,814
    Rep Power
    10
    This should be fixed in thinBasic 1.13.0.0 just released.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    Switch to ByVal for the parameters in your function.

  5. #5
    When you use ByRef, it passes a reference to the original variable, so any changes inside the function affect the original variable too. In your first code snippet, you're using the same reference for both elements of the array, which is why you're getting "Bacon, Bacon". If you switch to ByVal or make sure to use different variables for the function, it should print "Bacon, Tomato" as expected.

Similar Threads

  1. Function alias problem
    By xLeaves in forum thinBasic General
    Replies: 2
    Last Post: 11-07-2019, 19:33
  2. "Locate" Function For Delimited Strings
    By gungadout in forum thinBasic General
    Replies: 3
    Last Post: 12-10-2010, 12:24
  3. Problem Leaving Callback Function
    By gungadout in forum thinBasic General
    Replies: 12
    Last Post: 07-03-2010, 22:57
  4. Problem with returning a string from a function
    By DavidMead in forum Module SDK (Power Basic version)
    Replies: 4
    Last Post: 26-11-2008, 12:51
  5. Example: passing vector or array to same function
    By e90712 in forum Math scripts
    Replies: 2
    Last Post: 19-07-2008, 00:33

Members who have read this thread: 13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •