|
|
how to pass function as parameter
#431614
10/20/13 16:40
10/20/13 16:40
|
Joined: Apr 2006
Posts: 159 Latvija
Arrovs
OP
Member
|
OP
Member
Joined: Apr 2006
Posts: 159
Latvija
|
i want to put function as parameter for function, but how you can do it?
tried like this: function take_function(function parameter(void*))
and also tried like in C, but without success.
Arrovs once will publish game
|
|
|
Re: how to pass function as parameter
[Re: Arrovs]
#431616
10/20/13 17:08
10/20/13 17:08
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
User
|
User
Joined: Sep 2009
Posts: 993
Budapest
|
Function pointers Pointers of functions or actions can be defined as FUNCTION* under C-Script. LC Under lite-C a function pointer is defined just as a function prototype with return and parameter types. Example:
float myfunction(int a, float b); // define a function pointer named "myfunction"
float fTest(int a, float b) { return (a*b); }
...
myfunction = fTest;
x = myfunction(y,z);
For storing arrays of function pointers in lite-C, void* arrays can be used. Example:
float myfunction(int a, float b); // define a function pointer
void* function_array[100]; // define a pointer array
float fTest(int a, float b) { return (a*b); }
...
function_array[n] = fTest;
myfunction = function_array[n];
x = myfunction(y,z);
Last edited by Aku_Aku; 10/20/13 17:09.
|
|
|
Re: how to pass function as parameter
[Re: Aku_Aku]
#431618
10/20/13 18:12
10/20/13 18:12
|
Joined: Oct 2011
Posts: 1,082 Germany
Ch40zzC0d3r
Serious User
|
Serious User
Joined: Oct 2011
Posts: 1,082
Germany
|
Without parameters:
function execute_me()
{
error("lol");
}
function get_param_and_execute(void *pFunc)
{
function typedef_func();
typedef_func = pFunc;
typedef_func();
}
...
get_param_and_execute(execute_me);
With parameters:
function execute_me(STRING *strMsg)
{
error(strMsg);
}
function get_param_and_execute(void *pFunc, STRING *strMsg)
{
function typedef_func(STRING*);
typedef_func = pFunc;
typedef_func(strMsg);
}
...
get_param_and_execute(execute_me, "works!");
|
|
|
|
|
|