Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SOLVED Passing function with parameters as parameter

SOLVED Passing function with parameters as parameter

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsgame-devtutorialquestion
10 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    Since today is my day to ask stupid questions, here is another one. I have some understanding how to pass a function as a parameter. I can even add and use ONE parameter to such function. I do not understand the

    - syntax.

    how to add more parameters. What I am also missing is syntax on how to read / access , verify passed parameters. I need to make a decision based on the parameter.

    void OpenGL_Stencil(void (*f)(int parameter));
    void OpenGL_Stencil(void (*Stencil)(int parameter)) {
    Stencil(0); // pass parameter ??

    PS Take your time , my interned connection is like a yo-yo today.

    Greg UtasG L 2 Replies Last reply
    0
    • V Vaclav_

      Since today is my day to ask stupid questions, here is another one. I have some understanding how to pass a function as a parameter. I can even add and use ONE parameter to such function. I do not understand the

      - syntax.

      how to add more parameters. What I am also missing is syntax on how to read / access , verify passed parameters. I need to make a decision based on the parameter.

      void OpenGL_Stencil(void (*f)(int parameter));
      void OpenGL_Stencil(void (*Stencil)(int parameter)) {
      Stencil(0); // pass parameter ??

      PS Take your time , my interned connection is like a yo-yo today.

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      To add more parameters, just extend the argument list: (int parameter1, int parameter2) and so on. All you have at this point is a declaration and definition of the function OpenGL_Stencil. Its parameter is a function that returns void and takes one int parameter. The next step is to implement a function with that signature and pass it as an argument to OpenGL_Stencil.

      void Something(int value) { /* code */ }

      After which invoking OpenGL_Stencil(Something) should cause OpenGL_Stencil to invoke Something.

      Robust Services Core | Software Techniques for Lemmings | Articles

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      V 1 Reply Last reply
      0
      • Greg UtasG Greg Utas

        To add more parameters, just extend the argument list: (int parameter1, int parameter2) and so on. All you have at this point is a declaration and definition of the function OpenGL_Stencil. Its parameter is a function that returns void and takes one int parameter. The next step is to implement a function with that signature and pass it as an argument to OpenGL_Stencil.

        void Something(int value) { /* code */ }

        After which invoking OpenGL_Stencil(Something) should cause OpenGL_Stencil to invoke Something.

        Robust Services Core | Software Techniques for Lemmings | Articles

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        Yes, I got that far. I need to read the passed parameter.

        void OpenGL_Stencil(void (*Stencil)(int parameter)) {

        // clear all three buffers - not neccessary
        glClear(GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        //enable estecil test
        glEnable(GL_STENCIL_TEST);
        // turn off writing to color buffer
        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
        // enable only to verify the stencil
        //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
        // setup stencil function
        glStencilFunc(GL_ALWAYS, 1, 1); // ??
        // setup stencil opertion
        glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // ??
        // ??
        glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
        // get stencil
        // TDOD need parameter here
        //OpenGL_Unit_Circle(0);
        Stencil(0); // works with OpenGL_Unit_Circle(0) parameter
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
        //
        this is where I am missing how to test the parameter
        // switch keep area
        //Stencil->paramater == 0;
        // if (Stencil->paramater == 0)
        glStencilFunc(GL_EQUAL, 1, 1); ///keep inside circle
        // else
        // glStencilFunc(GL_NOTEQUAL, 1, 1); // keep all outside of stencil
        //
        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

        Greg UtasG 1 Reply Last reply
        0
        • V Vaclav_

          Yes, I got that far. I need to read the passed parameter.

          void OpenGL_Stencil(void (*Stencil)(int parameter)) {

          // clear all three buffers - not neccessary
          glClear(GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          //enable estecil test
          glEnable(GL_STENCIL_TEST);
          // turn off writing to color buffer
          glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
          // enable only to verify the stencil
          //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
          // setup stencil function
          glStencilFunc(GL_ALWAYS, 1, 1); // ??
          // setup stencil opertion
          glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // ??
          // ??
          glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
          // get stencil
          // TDOD need parameter here
          //OpenGL_Unit_Circle(0);
          Stencil(0); // works with OpenGL_Unit_Circle(0) parameter
          glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
          //
          this is where I am missing how to test the parameter
          // switch keep area
          //Stencil->paramater == 0;
          // if (Stencil->paramater == 0)
          glStencilFunc(GL_EQUAL, 1, 1); ///keep inside circle
          // else
          // glStencilFunc(GL_NOTEQUAL, 1, 1); // keep all outside of stencil
          //
          glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #4

          I'm not certain what you're asking here. Inside OpenGL_Stencil, the name of the parameter that was passed in (a function) is Stencil. If you're talking about the int parameter for Stencil, OpenGL_Stencil has to provide that itself. It isn't the parameter associated with Stencil, because that only describes Stencil's function signature and could actually be left out, the same as in a function declaration:

          void OpenGL_Stencil(void (*Stencil)(int))

          If OpenGL_Stencil doesn't know what value to pass to Stencil, you could provide it like this:

          void OpenGL_Stencil(void (*Stencil)(int), int value)

          after which you can invoke

          Stencil(value)

          Robust Services Core | Software Techniques for Lemmings | Articles

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          V 1 Reply Last reply
          0
          • Greg UtasG Greg Utas

            I'm not certain what you're asking here. Inside OpenGL_Stencil, the name of the parameter that was passed in (a function) is Stencil. If you're talking about the int parameter for Stencil, OpenGL_Stencil has to provide that itself. It isn't the parameter associated with Stencil, because that only describes Stencil's function signature and could actually be left out, the same as in a function declaration:

            void OpenGL_Stencil(void (*Stencil)(int))

            If OpenGL_Stencil doesn't know what value to pass to Stencil, you could provide it like this:

            void OpenGL_Stencil(void (*Stencil)(int), int value)

            after which you can invoke

            Stencil(value)

            Robust Services Core | Software Techniques for Lemmings | Articles

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            May try it this way the parameter is a function - Stencil such function is passed with its own parameter - such in Stencil(parameter) how do I access that parameter "value" in OpenGL_Stencil function? I need to do if(Stencil(parameter == x )

            Greg UtasG 1 Reply Last reply
            0
            • V Vaclav_

              May try it this way the parameter is a function - Stencil such function is passed with its own parameter - such in Stencil(parameter) how do I access that parameter "value" in OpenGL_Stencil function? I need to do if(Stencil(parameter == x )

              Greg UtasG Offline
              Greg UtasG Offline
              Greg Utas
              wrote on last edited by
              #6

              You have to pass it as a parameter to OpenGL_Stencil, the way I described in the second half of my post.

              Robust Services Core | Software Techniques for Lemmings | Articles

              <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
              <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

              V 1 Reply Last reply
              0
              • V Vaclav_

                Since today is my day to ask stupid questions, here is another one. I have some understanding how to pass a function as a parameter. I can even add and use ONE parameter to such function. I do not understand the

                - syntax.

                how to add more parameters. What I am also missing is syntax on how to read / access , verify passed parameters. I need to make a decision based on the parameter.

                void OpenGL_Stencil(void (*f)(int parameter));
                void OpenGL_Stencil(void (*Stencil)(int parameter)) {
                Stencil(0); // pass parameter ??

                PS Take your time , my interned connection is like a yo-yo today.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                the c programming language 3rd edition - Google Search[^]

                1 Reply Last reply
                0
                • Greg UtasG Greg Utas

                  You have to pass it as a parameter to OpenGL_Stencil, the way I described in the second half of my post.

                  Robust Services Core | Software Techniques for Lemmings | Articles

                  V Offline
                  V Offline
                  Vaclav_
                  wrote on last edited by
                  #8

                  OK, I think I have the concept - have passed function return value insed of being void. Now I am still not sure about the proper syntax. Here is my code

                     OpenGL\_Stencil\_Circle\_Build(parameter);         returns parameter OK 
                  
                     OpenGL\_Stencil(OpenGL\_Stencil\_Circle\_Build(6)); wrong syntax     
                  

                  And here is the error

                  Description Resource Path Location Type
                  Invalid arguments '
                  Candidates are:
                  int OpenGL_Stencil(void (*)(int))
                  ' A_STENCIL.cpp /A_STENCIL/src line 766 Semantic Error
                  invalid conversion from ‘int’ to ‘void (*)(int)’ [-fpermissive] A_STENCIL.cpp /A_STENCIL/src line 766 C/C++ Problem

                  The declaration may be the issue ? int OpenGL_Stencil(void (*f)(int parameter)); Could somebody please reply with correct syntax or help me with declaration ?

                  Greg UtasG 1 Reply Last reply
                  0
                  • V Vaclav_

                    OK, I think I have the concept - have passed function return value insed of being void. Now I am still not sure about the proper syntax. Here is my code

                       OpenGL\_Stencil\_Circle\_Build(parameter);         returns parameter OK 
                    
                       OpenGL\_Stencil(OpenGL\_Stencil\_Circle\_Build(6)); wrong syntax     
                    

                    And here is the error

                    Description Resource Path Location Type
                    Invalid arguments '
                    Candidates are:
                    int OpenGL_Stencil(void (*)(int))
                    ' A_STENCIL.cpp /A_STENCIL/src line 766 Semantic Error
                    invalid conversion from ‘int’ to ‘void (*)(int)’ [-fpermissive] A_STENCIL.cpp /A_STENCIL/src line 766 C/C++ Problem

                    The declaration may be the issue ? int OpenGL_Stencil(void (*f)(int parameter)); Could somebody please reply with correct syntax or help me with declaration ?

                    Greg UtasG Offline
                    Greg UtasG Offline
                    Greg Utas
                    wrote on last edited by
                    #9

                    I believe that this

                    invalid conversion from ‘int’ to ‘void (*)(int)

                    means that you are passing an int argument when a function argument is expected, specifically a function that takes one int parameter and returns void. The problem appears to be

                    OpenGL_Stencil(OpenGL_Stencil_Circle_Build(6))

                    where, based on previous posts in this thread, I assume you want OpenGL_Stencil to call OpenGL_Stencil_Circle_Build with a value of 6. If that's the case, you did not read my previous post very carefully, because you need to do it as follows:

                    OpenGL_Stencil(OpenGL_Stencil_Circle_Build, 6)

                    where OpenGL_Stencil is defined as

                    void OpenGL_Stencil(void (*func) (int), int arg)
                    {
                    //...
                    func(arg); // this is the line that will call OpenGL_Stencil_Circle_Build with a value of 6
                    }

                    Robust Services Core | Software Techniques for Lemmings | Articles

                    <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                    <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                    V 1 Reply Last reply
                    0
                    • Greg UtasG Greg Utas

                      I believe that this

                      invalid conversion from ‘int’ to ‘void (*)(int)

                      means that you are passing an int argument when a function argument is expected, specifically a function that takes one int parameter and returns void. The problem appears to be

                      OpenGL_Stencil(OpenGL_Stencil_Circle_Build(6))

                      where, based on previous posts in this thread, I assume you want OpenGL_Stencil to call OpenGL_Stencil_Circle_Build with a value of 6. If that's the case, you did not read my previous post very carefully, because you need to do it as follows:

                      OpenGL_Stencil(OpenGL_Stencil_Circle_Build, 6)

                      where OpenGL_Stencil is defined as

                      void OpenGL_Stencil(void (*func) (int), int arg)
                      {
                      //...
                      func(arg); // this is the line that will call OpenGL_Stencil_Circle_Build with a value of 6
                      }

                      Robust Services Core | Software Techniques for Lemmings | Articles

                      V Offline
                      V Offline
                      Vaclav_
                      wrote on last edited by
                      #10

                      It is now working, however, the mechanics are NOT what you have presented. The initial problem was to pass a parameter to the passed function such as Stencil(parameter); // parameter); Now in my poor English interpretation I have A function OpenGL_Stencil with first argument int (*Stencil)(int) and second argument int parameter The OpenGL_Stencil returns int which is not currently used. The first argument - function takes the second argument as a -parameter- and returns it. The returned value is used to switch the code which follows. That was the original task , however, I could just use the passed second argument to do the switching, without the use of the return value. But that is just the way I like to make sure the code is actually processing the first argument - the function. I believe my original misunderstanding was trying to pass the second argument - parameter _ as a argument to the first argument - the function. Next task - pass multiple arguments or a pointer.... Many thanks for your help, really appreciate it. Cheers.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups