SOLVED Passing function with parameters as parameter
-
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.
-
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.
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 functionOpenGL_Stencil
. Its parameter is a function that returnsvoid
and takes oneint
parameter. The next step is to implement a function with that signature and pass it as an argument toOpenGL_Stencil
.void Something(int value) { /* code */ }
After which invoking
OpenGL_Stencil(Something)
should causeOpenGL_Stencil
to invokeSomething
.Robust Services Core | Software Techniques for Lemmings | Articles
-
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 functionOpenGL_Stencil
. Its parameter is a function that returnsvoid
and takes oneint
parameter. The next step is to implement a function with that signature and pass it as an argument toOpenGL_Stencil
.void Something(int value) { /* code */ }
After which invoking
OpenGL_Stencil(Something)
should causeOpenGL_Stencil
to invokeSomething
.Robust Services Core | Software Techniques for Lemmings | Articles
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); -
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);I'm not certain what you're asking here. Inside
OpenGL_Stencil
, the name of the parameter that was passed in (a function) isStencil
. If you're talking about theint
parameter forStencil
,OpenGL_Stencil
has to provide that itself. It isn't theparameter
associated withStencil
, because that only describesStencil
'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
-
I'm not certain what you're asking here. Inside
OpenGL_Stencil
, the name of the parameter that was passed in (a function) isStencil
. If you're talking about theint
parameter forStencil
,OpenGL_Stencil
has to provide that itself. It isn't theparameter
associated withStencil
, because that only describesStencil
'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
-
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 )
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
-
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.
-
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
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++ ProblemThe declaration may be the issue ? int OpenGL_Stencil(void (*f)(int parameter)); Could somebody please reply with correct syntax or help me with declaration ?
-
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++ ProblemThe declaration may be the issue ? int OpenGL_Stencil(void (*f)(int parameter)); Could somebody please reply with correct syntax or help me with declaration ?
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 oneint
parameter and returnsvoid
. The problem appears to beOpenGL_Stencil(OpenGL_Stencil_Circle_Build(6))
where, based on previous posts in this thread, I assume you want
OpenGL_Stencil
to callOpenGL_Stencil_Circle_Build
with a value of6
. 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 asvoid 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
-
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 oneint
parameter and returnsvoid
. The problem appears to beOpenGL_Stencil(OpenGL_Stencil_Circle_Build(6))
where, based on previous posts in this thread, I assume you want
OpenGL_Stencil
to callOpenGL_Stencil_Circle_Build
with a value of6
. 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 asvoid 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
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.