Announcement

Collapse
No announcement yet.

Return a TYPE from a function call

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

  • Return a TYPE from a function call

    Lets say I want to call a function and send it a long and return a TYPE. What is the accepted way of handling that?

    for example: Lets say i have loaded up an array of my type and now i want to retrieve a value:

    snip ..

    TYPE FuncVarType
    TSPrice AS SINGLE
    SPr1 AS SINGLE
    SPn1 AS SINGLE
    Diff AS SINGLE
    SPr2 AS SINGLE
    END TYPE

    GLOBAL FuncVar AS FuncVarType
    GLOBAL FuncVarStack() AS FuncVarType
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    FUNCTION GetFuncVars(BYVAL LkBk AS LONG) AS FuncVarType ' Retrieve TYPE from the Push Stack

    IF LkBk > NumElem THEN MSGBOX("Error") : EXIT FUNCTION
    IF LkBk <= Index - 1 THEN
    FUNCTION = FuncVarStack(Index - 1 - LkBk)
    ELSE
    FUNCTION = FuncVarStack(NumElem + Index - LkBk)
    END IF
    END FUNCTION

    this will not compile because of
    FUNCTION GetFuncVars(BYVAL LkBk AS LONG) AS FuncVarType


    So how do you return a TYPE from a function?


    ------------------
    Kind Regards
    Mike

  • #2
    You can't return a type from a function. You can pass it as a parameter. Say:
    Code:
    Function GetFuncVars( BYVAL LkBk AS LONG, FuncVar AS FuncVarType ) as long
    You will use FuncVar inside the function, and get the modified values after the call. The return value can be ignored, or you can return a completion code, or something else...



    ------------------
    Rgds, Aldo

    Comment


    • #3
      Our use a static and return a pointer

      ------------------
      hellobasic

      Comment


      • #4
        STATIC variables should not generally be used for return buffers, as this will
        lead to problems if your function may be called recursively, from multiple threads,
        or in various other situations where the buffer may be changed before you have a
        chance to use it!

        ------------------
        Tom Hanlin
        PowerBASIC Staff

        Comment


        • #5
          You can return a string like this:

          FUNCTION GetFuncVars(BYVAL LkBk AS LONG) AS STRING ' Retrieve TYPE from the Push Stack
          'whatever
          END FUNCTION

          and call it like this:

          LSET MyType = GetFuncVars(SomeValue)


          Peter


          ------------------
          [email protected]
          [email protected]

          Comment


          • #6
            Ok thx,

            I think i like option A

            Aldo,

            This is passing the TYPE BYREF right? that way I can get the changed values from the calling function

            ------------------
            Kind Regards
            Mike

            Comment


            • #7
              UDTs are always passed BYREF, never BYVAL.

              ------------------
              Lance
              PowerBASIC Support
              mailto:[email protected][email protected]</A>
              Lance
              mailto:[email protected]

              Comment

              Working...
              X
              😀
              🥰
              🤢
              😎
              😡
              👍
              👎