Results 1 to 5 of 5

Thread: Problem with returning a string from a function

  1. #1

    Problem with returning a string from a function

    I have a simple function to return a string variable but I am getting error 252 "invalid delimter" when it returns but I can't see why it should fail.

    Is there something I am missing?

    FUNCTION Exec_STRTEST() AS STRING

    LOCAL strvar AS STRING

    strvar = "Return String"

    FUNCTION = strvar

    END FUNCTION

  2. #2
    thinBasic MVPs
    Join Date
    Sep 2006
    Posts
    3,289
    Blog Entries
    2
    Rep Power
    343

    Re: Problem with returning a string from a function

    Yip, looks right to me too. Does your line in LoadLocalSymbols look like this? Pay attention to the %thinBasic_ReturnString

    [code=thinbasic]

    thinBasic_LoadSymbol "Exec_STRTEST",%thinBasic_ReturnString,CodePtr(xExec_STRTEST),%thinBasic_ForceOverWrite



    [/code]


  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10

    Re: Problem with returning a string from a function

    Hi David,

    attached to this post an example module using your function. Also a thinkbasic script testing the module function STRTEST.

    Be sure to:
    • create your own functions (as you already did)
    • attach your function to a thinBasic keyword using thinBasic_LoadSymbol API function inside LoadLocalSymbols module function
    • attention to indicate in thinBasic_LoadSymbol the correct return code (usually %thinBasic_ReturnString or %thinBasic_ReturnNumber)


    Let me know.
    Ciao
    Eros
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  4. #4

    Re: Problem with returning a string from a function

    Thanks for the help. After looking at your program Eros I knew I must be doing something wrong so I went back and had a look and found where I was going wrong.

    Originally I was trying to pass some parameters into the function but had the problem so I reduced it to the simple test program and still had the problem.

    With the test program and no parameters I was calling the function with empty paranthesis which caused a failure. I should have been calling with no brackets if no parameters.

    With the program where I was passing a parameter I forgot to check the thinBasic_CheckCloseParens. When I add that it works ok.


  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10

    Re: Problem with returning a string from a function

    Ok, perfect.
    Let me know if you need any further help.

    Regarding ( and ) parenthesis, to let your syntax to optionally use ( and ), I usually do the following.

    Imagine you want a function with the following syntax:
    STRTEST (AnyString AS String, AnyNumber as LONG)

    you can parse it in this way:
    [code=thinbasic]
    '----------------------------------------------------------------------------
    Function Exec_STRTEST() As String
    ' Simple test function: STRTEST(AnyString, AnyNumber)
    '----------------------------------------------------------------------------
    Local MyStr As String
    Local MyNumber As Ext
    Local PP As Long 'pp stands for Parens Present

    '---Check if first parens was present and store it
    pp = thinBasic_CheckOpenParens_Optional

    '---Parse the first param (string)
    thinBasic_ParseString MyStr

    '---Check the mandatory comma
    If thinBasic_CheckComma_Mandatory Then

    '---Parse the second param (numeric)
    thinBasic_ParseNumber MyNumber

    '---If there was a ( than it is mandatory to have a )
    If pp Then thinBasic_CheckCloseParens_Mandatory

    '---If no error than we can procede
    If thinBasic_ErrorFree Then

    '--Do something with your data and return something else
    Function = Left$(MyStr, MyNumber)
    End If

    End If

    End Function
    [/code]

    The above will work for
    [code=thinbasic]STRTEST ("Any string", 2)[/code]
    but also for
    [code=thinbasic]STRTEST "Any string", 2[/code]
    giving more freedom to the thinBasic programmer

    Hope it can help.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Posting Permissions

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