0

Consider the below python code:

# Return a function that returns its argument incremented by 'n'
def make_incrementer(n):
    def increment(x, n=n):
        return x+n
    return increment

add1 = make_incrementer(1)
print add1(3)  # This prints '4'

Is there a way to do something similar in C? I want a function to return another function (local/nested or otherwise) that can be invoked by the user.

Just to be clear, I do not want to:

  • call another function within a function and return value of that computation
  • return a pointer to another function from within a function

Please note: I have looked into 'Is there a a way to achieve closures in C'. But it doesn't answer the question since all the answers given are kind of workarounds that rely heavily on pointers, external libraries etc. Also the question is quite old (9 years) and I am hoping some compiler out there may have added support/extension for doing something like this since then.

Ashesh
  • 3,499
  • 1
  • 27
  • 44
  • Do you mean returning a Python callable from a C function in the Python C API, or returning a C function in a regular C program that doesn't use Python? – user2357112 Feb 05 '20 at 05:09
  • See questions like [this one](https://stackoverflow.com/questions/4430156/how-do-i-create-a-c-factory-function). I think the term to look for is "C factory function" – Marius Feb 05 '20 at 05:09
  • You want closures, but C doesn't have closures. You'll need to return a struct with the value that would be captured (`n`) and a pointer to the function (`increment`). The closure , and the caller would have to pass (a pointer to) the struct to the function when it calls it. – ikegami Feb 05 '20 at 05:09
  • @user2357112supportsMonica I meant the second one ie. returning a C function in a regular C program that doesn't use Python. – Ashesh Feb 05 '20 at 05:10
  • This may help https://stackoverflow.com/a/20617739/5306039 – Shubham Feb 05 '20 at 05:20
  • Thank you for the comments. I have looked into '[Is there a a way to achieve closures in C](https://stackoverflow.com/questions/4393716/is-there-a-a-way-to-achieve-closures-in-c)'. But it doesn't answer the question. Also the question is quite old (9 years). – Ashesh Feb 05 '20 at 05:26
  • 5
    Re "*all the answers given are kind of workarounds*", C doesn't have closures. So yes, you're looking for a workaround. – ikegami Feb 05 '20 at 05:36
  • I am okay with workarounds as long as they ultimately return an invokeable function (not a function pointer). – Ashesh Feb 05 '20 at 05:38
  • 1
    `return an invokeable function (not a function pointer).` that does not make sense. How can you return a function? How can you return an invokeable function that is not a function pointer? If you return something and you then call it as a function that is exactly a function pointer. – KamilCuk Feb 05 '20 at 05:39
  • 3
    Re "*an invokeable function (not a function pointer).*", There's no difference between a function and what `add1` contains in your Python example. They're both pointers to a function. – ikegami Feb 05 '20 at 05:46
  • @ikegami your argument makes sense but in case of python the implementation details are hidden at lower level and hence I don't have to deal with additional constructs to invoke the function on some parameters. In C I would first retrieve the function itself (from the pointer) and then pass it arguments (on same or different lines). So there's some difference. – Ashesh Feb 05 '20 at 06:24
  • 1
    No, you can do `add1(3)` in both. There's not even a syntactical difference. – ikegami Feb 05 '20 at 06:25

0 Answers0