how to pass function as parameter

Posted By: Arrovs

how to pass function as parameter - 10/20/13 16:40

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.
Posted By: MasterQ32

Re: how to pass function as parameter - 10/20/13 17:00

Here you go
Code:
function take_function(void *fnPtr)
{
    function parameter(void* arg);
    parameter = fnPtr;
}

Posted By: Aku_Aku

Re: how to pass function as parameter - 10/20/13 17:08

Originally Posted By: Gamestudio Manual
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:
Code:
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:
Code:
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);


Posted By: Ch40zzC0d3r

Re: how to pass function as parameter - 10/20/13 18:12

Without parameters:
Code:
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:
Code:
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!");

Posted By: Arrovs

Re: how to pass function as parameter - 10/21/13 16:29

thanks.
© 2024 lite-C Forums