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.