Can we return a function in C ++?

I have a program that contains three functions: main()

, returnFunction()

and functionToBeReturned()

. main()

should call returnFunction()

. Can we use it functionToBeReturned()

as a return value returnFunction()

?

int functionToBeReturned()
{
    // some code here....
    return value;
}

int returnFunction()
{
    // some code here....
    return functionToBeReturned();
}

int main()
{
    returnFunction();
}

      

This is normal...

+3


source to share


2 answers


In C, you can have and return function pointers; they basically point to machine code that you can call. You might have some library functions capable of "creating" functions (for example, dynamically generating or loading some machine code) and giving a pointer to the newly generated code ( dlsym

, JIT libraries, ....) But in pure C99 or C11 standard or C ++ 11 has no way to create raw simple functions (at the machine code level) at runtime. In a program limited by standard C ++ or C, the set of functions is fixed (and that is the set of values ​​that all pointer functions other than the pointer can see NULL

). Since C does not have proper closures, many C frameworks (e.g. GTK or its Glib, libevent, ....) handle callbacks implemented with a function pointer to mix conditionals and client data (e.g. read the GTK tutorial , see g_signal_connect , ...)

In C ++ (C ++ 11 or newer), you can also close , anonymous functions (like lambdas) and std::function

; eg

 std::function<int(int)> translation(int delta) {
    return [delta](int x) { return x + delta; };
 }

      

In the returned closure (which semantically behaves almost like a function, since you can call and apply it) delta

is the closed value.



Closure is an extremely important and complex concept related to lambda calculus and functional programming . Don't be surprised that it takes you weeks to figure this out. See Also Scheme , Lisp In Small Parts , SICP ....

This answer to a fairly similar question provides much more detail.

At the machine level, a pointer to a C or C ++ function is very often some address pointing to a segment of code but C ++ closure is some internal object (of some "anonymous" class, internal to your compiler's implementation), mixing of code pointers and private values or links.

Learning some functional programming languages ​​like Ocaml or Scheme or Clojure or Haskell or Common Lisp will improve your thinking (even when coding in C or C ++).

+4


source


Yes! Function pointers are part of C and C ++. Using function pointers, you can return functions from other functions and store them in classes, etc. Here is an article about them.

#include <iostream>
using namespace std;
int functionToBeReturned() {
    cout << "hello!" << endl;
}
int (*returnFunction())() {
    // Observe how the parentheses around returnFunction distinguish a function
    // returning a function pointer from a normal function.
    return functionToBeReturned;
}
int main() {
    int (*function)();
    function = returnFunction();
    function();
}

      

In this program returnFunction

, it is a function that accepts nothing and returns a function that accepts nothing and returns an int. Function pointer declarations are really ugly in C and C ++! You can do this for clarity:

typedef int (*returns_int_t)();

      



This defines a name returns_int_t

for a pointer to a function that accepts nothing and returns an int. Then you can do:

#include <iostream>
using namespace std;

typedef int (*returns_int_t)();

int functionToBeReturned() {
    cout << "hello!" << endl;
}
returns_int_t returnFunction() {
    return functionToBeReturned;
}
int main() {
    returns_int_t function = returnFunction();
    function();
}

      

Which does the same.

+3


source







All Articles