Announcement

Collapse
No announcement yet.

Init values in functions called BYREF

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

  • Init values in functions called BYREF


    Be sure to init values using BYREF (the default.)
    This might be noted in the docs. I knew better, just bringing it up.
    Code:
    FUNCTION PBMAIN () AS LONG
     
        LOCAL x, y, z AS LONG
        x = 99: y = 100: z = 101
        CALL test (x, y, z)
        ? "Did you expect" +  STR$(x) + "," + STR$(y) + "," + STR$(z)
     
    END FUNCTION
     
    FUNCTION Test(a AS LONG, b AS LONG,c AS LONG) AS LONG
     
    END FUNCTION
    Last edited by Mike Doty; 19 Sep 2009, 10:55 AM.

  • #2
    Yes, I expected 99,100 and 101, and that's what I got. (PB/Win 9.0.1)

    What did you expect?
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      I expected you.

      Comment


      • #4
        Well, you got me. Now what?
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Then again...
          Syntax
          [STATIC] SUB ProcName [BDECL | CDECL | SDECL] [ALIAS "AliasName"] [([arguments])] [EXPORT | PRIVATE] [STATIC]

          [LOCAL variable_list]

          [STATIC variable_list]

          {statements}

          [EXIT SUB]

          {statements}

          END SUB
          ... the compiler failed to detect the absence of any {statements}, ( "{}" means REQUIRED; "[]" are optional ), so I guess I was lucky my variables weren't corrupted.

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            I mentioned this because it took me some time to debug some code and wondered if the help file mentioned unused variables are
            not init. This is from the help file on the CALL statement. I think it might be expanded.

            When a parameter is passed by reference, it consists of a 4-byte address of the data. In this case, the original data can be modified by the procedure.

            Comment


            • #7
              But you aren't modifying anything (the test function has no code). What did you expect? That the variables will be set to 0?
              Forum: http://www.jose.it-berater.org/smfforum/index.php

              Comment


              • #8
                No, I expect others to expect that since it is not mentioned in the docs.
                I've known about it for many years and just thought it should be mentioned. It took me too long to find an error and should have
                been looking for this. Maybe I should not have brought this up.

                Comment


                • #9
                  >Maybe I should not have brought this up

                  If the documentation was confusing to you, then of course you should bring it up. But you really should report this to [email protected] and call it a "documentation improvement suggestion."

                  It can get in line behind the 863 such suggestions I have sent in and have been "referred to the Documentation Team."

                  (What, you thought the New York Mets were the most overpaid team in this country?)

                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    The documentation was not confusing, but limited.
                    I've sent them a bunch this week.

                    I had to look in my outbound emails.
                    I did submit this one.

                    Thanks

                    Comment


                    • #11
                      > The documentation was not confusing, but limited.

                      What's the difference?

                      Whenever someone has to ask what the documentation is really saying, something is wrong.

                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        >and wondered if the help file mentioned unused variables are not init.

                        Hmm... you have no unused variables.

                        In WinMAin, X, Y and Z are used both in assignment statements, as parameters to the test procedure, and arguments to the MSGBOX function.

                        The illegal-but-apparently-acceptable-nonetheless procedure 'Test' has no variables.

                        Wait a minute... you were not somehow lead to believe variable parameters passed by reference were initialized on entry to a procedure, were you?
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Originally posted by Michael Mattias View Post
                          >Wait a minute... you were not somehow lead to believe variable parameters passed by reference were initialized on entry to a procedure, were you?
                          Is this thread a joke. Three variables are passed BYREF to a function that doesn't change them and the calling program demonstrates that checked in a couple versions of the compiler.
                          I don't understand why there is a discussion about initialisation. Of course the compiler does it automatically, X, Y and Z are local variables which the compiler automatically initialises to zero RTFM, if the program does not set a value then that value of zero would be passed BYREF and as they are not changed then they would remain at zero. How can a variable, passed BYREF be in any other way "initialised", thats done when the variable is created, not when it is passed.
                          Update the documentation? You are joking, what replace a set of curly brackets with square brackets? Picky, Picky. I have sent a couple of suggestions for documentation improvements but this is getting to a rediculous level.
                          To be honest I read this thread many times before replying as I kept thinking it had to be an old one dated April 1.

                          Comment


                          • #14
                            Update the documentation? You are joking, what replace a set of curly brackets with square brackets?
                            Well, that may not be a documentation error; it may in fact be a compiler error.

                            The doc says {statements} is required, yet the compiler compiled it in spite of the fact the procedure 'Test' contains no statements.

                            Either...
                            A. The documentation is in error
                            OR
                            B. The compiler should have refused to compile the program, failing on a "Required statement(s) missing" error.

                            MCM
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              0 or more

                              Originally posted by Michael Mattias View Post
                              Then again...


                              ... the compiler failed to detect the absence of any {statements}, ( "{}" means REQUIRED; "[]" are optional ), so I guess I was lucky my variables weren't corrupted.

                              MCM
                              Actually, NO brackets "[]" or "{}" means REQUIRED.

                              {} means 0 or more. [] means optional, 0 or 1 .

                              So a function can have 0 statements and is completely valid.

                              Comment


                              • #16
                                >{} means 0 or more. [] means optional, 0 or 1 .

                                Really? That would explain a lot ot what I see here eg
                                Code:
                                IF integer_expression THEN
                                  {statements}
                                [ELSEIF integer_expression THEN
                                  {statements}]
                                [ELSE
                                  {statements}]
                                END IF
                                We often see
                                Code:
                                 IF   integer_expression
                                 REM                 <<<<  no statement here  
                                 ELSE 
                                     statement(s)
                                 END IF
                                However, the only reference to "{}" I can find in help is
                                Brackets [ ]
                                Indicates the information they enclose is optional. For example:

                                SEEK [#] filenum&, position&&

                                Braces { }
                                Indicates a choice of two or more options, one of which MUST be used. For example:

                                #DIM {ALL | NONE}
                                But if "{}" indicates at least one, then references to {statements} here should mean "one" since {statements} itself is plural.

                                Oh well, it's not as though it's the true meaning of "{}" is what drove the help file off the short list for one of the Pulitzer prizes.

                                Too bad you can't find (well, I can't find) a comprehensive "syntax conventions" page in the help file.

                                MCM
                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                • #17
                                  This code compile and run w/o error

                                  Code:
                                  #COMPILE EXE
                                  #DIM ALL
                                  #DEBUG ERROR ON
                                  
                                  FUNCTION PBMAIN () AS LONG
                                                          
                                      ON ERROR RESUME NEXT
                                      
                                      IF 1 THEN
                                          'nothing here  {statements}
                                      ELSEIF 2 THEN
                                          'nothing here  {statements}]
                                      ELSE
                                          'nothing here too {statements}]
                                      END IF
                                      
                                      ? ERROR$(ERR)
                                  
                                  END FUNCTION
                                  "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

                                  Comment


                                  • #18
                                    >This code compile and run w/o error

                                    Yes, but SHOULD it?
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      Yes, Arthur's code should compile and run and yes, there are several places in the documentation that the syntax is incorrect. Whenever I find one, I send it in to support.
                                      Jeff Blakeney

                                      Comment


                                      • #20
                                        I have logged this with our documentation team. I have found the following topics that need to be updated:
                                        Code:
                                        #IF metastatement
                                        Do/Loop block
                                        For/Next block
                                        Function/End Function block
                                        IF/END IF block
                                        Method/End Method block
                                        Property/End Property block
                                        Sub/End Sub block
                                        Try/End Try block
                                        While/Wend block
                                        If you find any not on this list, please send an email to support @ powerbasic . com. Thank you!
                                        Sincerely,

                                        Steve Rossell
                                        PowerBASIC Staff

                                        Comment

                                        Working...
                                        X