Higher-order functions: Difference between revisions
No edit summary |
|||
Line 66: | Line 66: | ||
Of course, in a real project you shouldn't write such a convoluted code, but use some typedef instead, in order to break complexity into steps. |
Of course, in a real project you shouldn't write such a convoluted code, but use some typedef instead, in order to break complexity into steps. |
||
==[[Perl]]== |
|||
[[Category:Perl]] |
|||
'''Interpreter:''' [[Perl]] |
|||
==[[C plus plus|C++]]== |
==[[C plus plus|C++]]== |
Revision as of 16:30, 24 January 2007
You are encouraged to solve this task according to the task description, using any language you may know.
Pass a function as an argument.
C
Simple example, the pointer to the function to be passed as an argument is the only involved pointer.
Definition of a function whose only parameter is a pointer to a function with no parameters and no return value:
void myFuncSimple( void (*funcParameter)(void) ) { /* ... */ *funcParameter(); /* Call the passed function. */ /* ... */ }
Call:
void funcToBePassed(void); /* ... */ myFuncSimple(&funcToBePassed);
Complex example.
Definition of a function whose return value is a pointer to int and whose only parameter is a pointer to a function, whose (in turn) return value is a pointer to double and whose only parameter is a pointer to long.
int* myFuncComplex( double* (*funcParameter)(long* parameter) ) { long* inLong; double* outDouble; /* ... */ outDouble = *funcParameter(inLong); /* Call the passed function and store returned pointer. */ /* ... */ }
Call:
double* funcToBePassed(long* parameter); /* ... */ int* outInt; outInt = myFuncComplex(&funcToBePassed);
Finally, declaration of a pointer variable of the proper type to hold such a function as myFunc:
int* (*funcPointer)( double* (*funcParameter)(long* parameter) ); /* ... */ funcPointer = myFuncComplex;
Of course, in a real project you shouldn't write such a convoluted code, but use some typedef instead, in order to break complexity into steps.
Perl
Interpreter: Perl
C++
Same as C.