In awk, when is a function definition executed?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












In awk, when is a function definition executed?



Is a function definition executed even before BEGIN ... is executed? Thanks.










share|improve this question

























    up vote
    0
    down vote

    favorite












    In awk, when is a function definition executed?



    Is a function definition executed even before BEGIN ... is executed? Thanks.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      In awk, when is a function definition executed?



      Is a function definition executed even before BEGIN ... is executed? Thanks.










      share|improve this question













      In awk, when is a function definition executed?



      Is a function definition executed even before BEGIN ... is executed? Thanks.







      awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Tim

      24.9k70239434




      24.9k70239434




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:



          $ cat go.awk
          BEGIN
          print "The BEGIN block is executing"
          print reverse("The BEGIN block is executing")



          print reverse($0)


          function reverse(str)
          trs=""
          for(i=length(str); i > 0; i--)
          trs=trs substr(str, i, 1);

          return trs



          And with:



          $ cat input
          line one
          line two
          line three


          ... the result is:



          $ awk -f go.awk < input
          The BEGIN block is executing
          gnitucexe si kcolb NIGEB ehT
          eno enil
          owt enil
          eerht enil


          You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.



          To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.



          The POSIX specification for awk says, most pertinently:




          A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.




          The gawk man page implies something similar:




          ... Gawk reads the program text as if all the program-files and com‐
          mand line source texts had been concatenated together. This is useful for
          building libraries of AWK functions, without having to include them in each
          new AWK program that uses them. It also provides the ability to mix library
          functions with command line programs.



          ...



          Gawk executes AWK programs in the following order. First, all variable
          assignments specified via the -v option are performed. Next, gawk compiles
          the program into an internal form. Then, gawk executes the code in the BEGIN
          rule(s) (if any), and then proceeds ...







          share|improve this answer




















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481821%2fin-awk-when-is-a-function-definition-executed%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:



            $ cat go.awk
            BEGIN
            print "The BEGIN block is executing"
            print reverse("The BEGIN block is executing")



            print reverse($0)


            function reverse(str)
            trs=""
            for(i=length(str); i > 0; i--)
            trs=trs substr(str, i, 1);

            return trs



            And with:



            $ cat input
            line one
            line two
            line three


            ... the result is:



            $ awk -f go.awk < input
            The BEGIN block is executing
            gnitucexe si kcolb NIGEB ehT
            eno enil
            owt enil
            eerht enil


            You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.



            To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.



            The POSIX specification for awk says, most pertinently:




            A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.




            The gawk man page implies something similar:




            ... Gawk reads the program text as if all the program-files and com‐
            mand line source texts had been concatenated together. This is useful for
            building libraries of AWK functions, without having to include them in each
            new AWK program that uses them. It also provides the ability to mix library
            functions with command line programs.



            ...



            Gawk executes AWK programs in the following order. First, all variable
            assignments specified via the -v option are performed. Next, gawk compiles
            the program into an internal form. Then, gawk executes the code in the BEGIN
            rule(s) (if any), and then proceeds ...







            share|improve this answer
























              up vote
              1
              down vote













              This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:



              $ cat go.awk
              BEGIN
              print "The BEGIN block is executing"
              print reverse("The BEGIN block is executing")



              print reverse($0)


              function reverse(str)
              trs=""
              for(i=length(str); i > 0; i--)
              trs=trs substr(str, i, 1);

              return trs



              And with:



              $ cat input
              line one
              line two
              line three


              ... the result is:



              $ awk -f go.awk < input
              The BEGIN block is executing
              gnitucexe si kcolb NIGEB ehT
              eno enil
              owt enil
              eerht enil


              You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.



              To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.



              The POSIX specification for awk says, most pertinently:




              A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.




              The gawk man page implies something similar:




              ... Gawk reads the program text as if all the program-files and com‐
              mand line source texts had been concatenated together. This is useful for
              building libraries of AWK functions, without having to include them in each
              new AWK program that uses them. It also provides the ability to mix library
              functions with command line programs.



              ...



              Gawk executes AWK programs in the following order. First, all variable
              assignments specified via the -v option are performed. Next, gawk compiles
              the program into an internal form. Then, gawk executes the code in the BEGIN
              rule(s) (if any), and then proceeds ...







              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:



                $ cat go.awk
                BEGIN
                print "The BEGIN block is executing"
                print reverse("The BEGIN block is executing")



                print reverse($0)


                function reverse(str)
                trs=""
                for(i=length(str); i > 0; i--)
                trs=trs substr(str, i, 1);

                return trs



                And with:



                $ cat input
                line one
                line two
                line three


                ... the result is:



                $ awk -f go.awk < input
                The BEGIN block is executing
                gnitucexe si kcolb NIGEB ehT
                eno enil
                owt enil
                eerht enil


                You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.



                To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.



                The POSIX specification for awk says, most pertinently:




                A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.




                The gawk man page implies something similar:




                ... Gawk reads the program text as if all the program-files and com‐
                mand line source texts had been concatenated together. This is useful for
                building libraries of AWK functions, without having to include them in each
                new AWK program that uses them. It also provides the ability to mix library
                functions with command line programs.



                ...



                Gawk executes AWK programs in the following order. First, all variable
                assignments specified via the -v option are performed. Next, gawk compiles
                the program into an internal form. Then, gawk executes the code in the BEGIN
                rule(s) (if any), and then proceeds ...







                share|improve this answer












                This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:



                $ cat go.awk
                BEGIN
                print "The BEGIN block is executing"
                print reverse("The BEGIN block is executing")



                print reverse($0)


                function reverse(str)
                trs=""
                for(i=length(str); i > 0; i--)
                trs=trs substr(str, i, 1);

                return trs



                And with:



                $ cat input
                line one
                line two
                line three


                ... the result is:



                $ awk -f go.awk < input
                The BEGIN block is executing
                gnitucexe si kcolb NIGEB ehT
                eno enil
                owt enil
                eerht enil


                You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.



                To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.



                The POSIX specification for awk says, most pertinently:




                A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.




                The gawk man page implies something similar:




                ... Gawk reads the program text as if all the program-files and com‐
                mand line source texts had been concatenated together. This is useful for
                building libraries of AWK functions, without having to include them in each
                new AWK program that uses them. It also provides the ability to mix library
                functions with command line programs.



                ...



                Gawk executes AWK programs in the following order. First, all variable
                assignments specified via the -v option are performed. Next, gawk compiles
                the program into an internal form. Then, gawk executes the code in the BEGIN
                rule(s) (if any), and then proceeds ...








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                Jeff Schaller

                35.9k952119




                35.9k952119



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481821%2fin-awk-when-is-a-function-definition-executed%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown






                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)