Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a function that returns a function?

Big picture: I have a module with functions and a module with procedures and functions over those functions.

When I combine two functions (from function's module interface):

double f1(double alpha, double x); double f2(double beta, double x); 

In several ways, (one of them is adding):

double OP_Addition(double (*f)(double,double) , double (*g)(double,double), double param1, double param2, double x); 

Gives no problem with the following (piece of) implementation:

z1 = (*f)(param1, x); z2 = (*g)(param2, x); y = z1 + z2; return y; 

But when I want to return a pointer to a "new" function, something like:

void *OP_PAdd( double (*f)(double,double), double param3 ); 

I can not get it to work properly, neither make the right "call". I want to have use the output "function" as input in other functions.

like image 540
MCL Avatar asked Jun 11 '16 19:06

MCL


People also ask

Can a Python function return a function?

Python may return functions from functions, store functions in collections such as lists and generally treat them as you would any variable or object. Defining functions in other functions and returning functions are all possible.

How do I return a value from a function to another function?

Call the function and save the return value of that very call. function firstFunction() { // do something return "testing 123"; } var test = firstFunction(); // this will grab you the return value from firstFunction(); alert(test);

Can a function return another function JS?

Functions that accept functions that return functions are common for any js code. Let's start: Functions are the same data as numbers or strings, so functions can be passed to other functions as arguments, as well as returned from functions. We can even define a function inside another function and return it outside.

How do you call a function return another function in Python?

Simply call a function to pass the output into the second function as a parameter will use the return value in another function python.


2 Answers

When returning a function from another function, the cleanest way to do this is with a typedef:

typedef double (*ftype)(double, double); 

Then you can declare your function like this:

ftype OP_PAdd( ftype f, double param3 ) {     ....     return f1; } 

You can do this without a typedef, but it's messy:

double (*OP_PAdd( double (*f)(double,double), double param3 ))(double,double) {     return f1; } 

So when you have function pointers as either parameters or return values of other functions, use a typedef.

EDIT:

While you could declare the type like this:

typedef double ftype(double, double); 

You can never directly use a type like this in practice. A function can't return a function (only a pointer to a function), and a variable of this type can't be assigned to.

Also, you don't need to explicitly dereference a function pointer to call the function, so the fact that the pointer itself is hidden is not a big issue. It's also convention to define function pointers as a typedef. From the man page for signal:

   #include <signal.h>     typedef void (*sighandler_t)(int);     sighandler_t signal(int signum, sighandler_t handler); 
like image 138
dbush Avatar answered Oct 11 '22 04:10

dbush


Other answers are correct and interesting, but you should be aware that in portable C99, there is no way to have genuine closures as C functions (and this is a fundamental limitation of C). If you are not aware of what closures are, read the wiki page carefully on them (and also read SICP, notably its §1.3). Notice however that in C++11 you do have closures, using std::function and lambda-expressions. And most other programming languages (Ocaml, Haskell, Javascript, Lisp, Clojure, Python, ....) have closures.

Because of the lack of genuine closures in C ("mathematically" the only closed values in C functions are global or static variables or literals), most libraries accepting C function pointers provide an API handling callbacks with some client data (a simple example could be qsort_r, but more seriously look inside GTK). That client data (generally an opaque pointer) can be used to keep closed values. You probably want to follow a similar convention (so systematically pass function pointer as callbacks with some additional client data), so you'll need to change the signatures of your C functions (instead of passing just a raw function pointer, you'll pass both a function pointer and some client data as a callback, to "emulate" closures).

You could sometimes generate a C function at runtime (using non-standard features, probably with the help of the operating system or some external library). You might use some JIT compiling library such as GNU lightning, libjit (both would generate some slow-running code quickly), asmjit (you'll generate each machine instruction explicitly, and it is your responsibility to emit fast x86-64 code), GCCJIT or LLVM (both are above existing compilers so can be used to emit -a bit slowly- some optimized code). On POSIX & Linux systems, you could also emit some C code in some temporary file /tmp/tempcode.c, fork a compilation (e.g. gcc -fPIC -Wall -O2 -shared /tmp/tempcode.c -o /tmp/tempcode.so) of that code into a plugin, and dynamically load that generated plugin using dlopen(3) & dlsym(3)..

BTW we don't know what the actual application you are coding is, but you might consider embedding inside it some interpreter, e.g. Lua or Guile. You'll then use and provide callbacks to the embedded evaluator/interpreter.

like image 27
Basile Starynkevitch Avatar answered Oct 11 '22 06:10

Basile Starynkevitch