3

I have a program containing three functions: main(), returnFunction() and functionToBeReturned(). main() has to call returnFunction(). Can we use functionToBeReturned() as a return value of returnFunction()?

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

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

int main()
{
    returnFunction();
}

Is that okay...

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • 6
    Out of curiosity, did you *try it* ? – WhozCraig Jul 15 '15 at 03:14
  • 3
    Currently, `returnFunction` returns the same thing as `functionToBeReturned`, ie. `value` (and to get `value`, `returnFunction` calls `functionToBeReturned`). If you want to return `functionToBeReturned` so that it can be called in main, that´s different (but possible too) – deviantfan Jul 15 '15 at 03:16
  • @-deviantfan Suppose I declare functionToBeReturned() as a boolean return type function. Now functionToBeReturned() is passing a boolean value. returnFunction() remains of int type. What's then? –  Jul 15 '15 at 03:24
  • 1
    @user5117637 Then `returnFunction` calls `functionToBeReturned` to get a `bool` which is then automatically converted to `int` (it´s a bigger type etc. no problem here), and then it´s returned. A boolean `true` will be 1, a `false` 0. About returning the function itself, if that is what you want, see brenns answer below. – deviantfan Jul 15 '15 at 03:26
  • if your signatures are all the same it is easy with function pointers. if not, you can use `closures` with C++11 `std::function` or `boost::function`. otherwise you also have libsignals++ – v.oddou Jul 15 '15 at 03:31
  • It may be useful to consider exactly in what order you want things to be called. Can you amend the example with `std::cout << "In function ..."` and the expected output? – MSalters Jul 15 '15 at 09:26
  • Very close to [this question](http://stackoverflow.com/q/31387238/841108) and [my answer](http://stackoverflow.com/a/31387444/841108) – Basile Starynkevitch Jul 15 '15 at 09:31
  • Is returning "a function" what you actually want to do or is the question about the function call in the return statement? – Pixelchemist Jul 15 '15 at 10:37
  • It's about using a function call as a return statement. –  Jul 15 '15 at 12:00
  • @whozcraig Yes...I tried it. Results were same as told by deviantfan. –  Jul 15 '15 at 16:31

2 Answers2

3

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 able to "create" functions (e.g. dynamically generate or load some machine code) and give a pointer to the newly created code (dlsym, JIT libraries, ....) But, in pure standard C99 or C11 or C++11, there is no way to create raw plain functions (at the machine code level) at runtime. In a program restricted to standard C++ or C, the set of functions is fixed (and is the set of values all pointer functions can get, in addition of the NULL pointer). Since C is lacking proper closures, many C frameworks (e.g. GTK or its Glib, libevent, ....) deal with callbacks, implemented by a convention mixing function pointer and client data (for example, read the GTK tutorial, see g_signal_connect, ...)

In C++ (C++11 or newer), you can also have closures, anonymous functions (i.e. lambda-s), and std::function; for example

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

In the returned closure (which semantically behaves almost as a function, since you can call and apply it), the delta is a closed value.

Closures are an extremely important and difficult concept, related to lambda calculus and functional programming. Don't be surprised to need weeks to understand it. See also Scheme, Lisp In Small Pieces, SICP....

This answer to a quite similar question gives much more details.

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

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

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

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 is a function that takes nothing and returns a function that takes nothing and returns an int. Declarations involving function pointers are really ugly in C and C++! You might consider doing this for clarity:

typedef int (*returns_int_t)();

This defines the name returns_int_t for a pointer to a function, which takes nothing and returns an int. Then, you could 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 exact same thing.

brenns10
  • 3,109
  • 3
  • 22
  • 24
  • 3
    Returning a function pointer isn't the same thing as returning the function itself (which isn't legal in C++). In practice, it's far more useful to use function objects (`std::function`, `std::bind`, lambdas) than it is to use function pointers. – isekaijin Jul 15 '15 at 03:45
  • 2
    Very good point! I've been using much more C than C++ lately, so I'm used to the reduced feature set. – brenns10 Jul 15 '15 at 03:51