Announcement

Collapse
No announcement yet.

Can FUNCTIONS return fixed length STRING or ASCIIZ?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can FUNCTIONS return fixed length STRING or ASCIIZ?

    Can FUNCTIONS return fixed length STRING or ASCIIZ?
    I always assumed since a function can return a string it could return a fixed length string...

  • #2
    No.

    I was pretty sure Help would back me up on it, but it's not under the topic "Function". Even so, I'm pretty sure it's documented somewhere.

    I ran a quick test and the compiler burps at doing such a thing.

    Comment


    • #3
      Gary got back while I was checking.
      Code:
      #compile exe
      #dim all
      function pbmain () as long
        local MyStr as string * 50
        MyStr = StrRetTest
        ? MyStr
      end function
      function StrRetTest() as string '* 50 'will not compile if take out 1st "'"
        'can return to a fixed string variable though!
        function = "Hello there!"
      end function
      Also take note of what help says about quad returns in DLL.

      Cheers.
      Dale

      Comment


      • #4
        I remember stumbling over this some time ago. I'm trying to get into the habit of having functions only return a true/false value, and passing the variable that should receive the function value BYREF in the call:

        Code:
        #DIM ALL
        
        %false = 0
        %true = NOT(%false)
        
        FUNCTION PBMAIN () AS LONG
        
            LOCAL Results  AS STRING * 5
        
            IF TestFunc(5, "target string", Results) THEN
                ? "results: '" & Results & "'"
            ELSE
                ? "Function failed"
            END IF
        
        END FUNCTION
        
        FUNCTION TestFunc(BYVAL l AS LONG, BYVAL v AS STRING, BYREF Results AS STRING * 5) AS LONG
        
            IF l < 1 THEN
                FUNCTION = %false
            ELSE
                results = LEFT$(v,l)
                FUNCTION = %true
            END IF
        END FUNCTION
                                      
        Real programmers use a magnetized needle and a steady hand

        Comment


        • #5
          Originally posted by David Clarke View Post
          Can FUNCTIONS return fixed length STRING or ASCIIZ?
          I always assumed since a function can return a string it could return a fixed length string...
          NO. In any programming language. Since they are fixed, you can only return a pointer to them, A string can be returned because what it returns is a copy, not the original. You can return a fixed lenght string if the return type of the function is AS STRING, in which case a copy will be returned.
          Forum: http://www.jose.it-berater.org/smfforum/index.php

          Comment


          • #6
            Just keep in mind that a basic dynamic string is passed as an address in OLE memory so you can use it as a "can" to return string data. Really depends on what you want at the receiving end.

            You can return a direct address, if the data is obtained in the function and written to allocated memory, you can use basic dynamic memory as a buffer and use STRPTR to get its address and at the function return end just return the basic dynamic string.

            Tell us a bit more on what you are doing and there is probably a way to do it.

            hutch at movsd dot com
            The MASM Forum

            www.masm32.com

            Comment


            • #7
              Thanks everybody!

              Comment

              Working...
              X