What is a callback in programming? - callback

I have heard this term tossed around, can anybody explain what it means and what problem it solves. Also where does this originate from.

Simplified it's when you pass a function to another function, and that other function call you back using the function you passed.
It's useful for asynchronous programming, when events can happen at any time. Or for generic handling of certain functionality to make some algorithms more generic (for example in C++ you can pass a comparison function to the std::sort function, and your comparison function will be used to compare two items when sorting).

Related

Scala legacy code: how to access input parameters at different points in execution path?

I am working with a legacy scala codebase, and as is always the case modifying the code is quite difficult without touching different parts.
One of my new requirement in to make several decisions based on some input parameters. Problem is that these decisions are to be made at various points along the execution. So either I encapsulate all those parameters in a case class instance and pass it along. But it means I would have to modify multiple methods signatures, and I want to avoid this approach as much as possible.
Another approach can be to create a global object containing all those input parameters and accessible from different points in the execution. Is it a good approach in Scala?
No, using global mutable variables to pass “hidden” parameters is not a good idea, not in Scala and not in any other programming language. It makes the code hard to understand and modify, because a function's behaviour will now depend on which functions were invoked earlier. And it's extremely fragile, because you might forget setting one of those global parameters before invoking the function, which means that it will use whatever value was stored there before. This is the kind of thing that can appear to work for years, and then break when you modify a completely unrelated part of the program.
I can't stress this enough: do not use global mutable variables, period. The solution is to man up and change those method signatures. Depending on the details, dependency injection may or may not help in your particular case.

Function which one must not shadow in Matlab

I am writing some helper functions for unit testing in Matlab. For that I need to overload Matlab functions, built-ins etc. In the documentation is written, that one shall not overload the function builtin. I also found out that one must not overload the lt operator, since it seems to get called by the UI all the time.
Is there a list of functions which one shall not overload, or do you know of any particular problems from overloading some specific functions?
Further Information:
My use case: I want to do mutation-testing and faul-injection.
in ther words, sometimes the called functions shall return wrong results or throw an error. If I overload the Matlab functions, I do not have to change the source code to do that.

Using only closures over functions in Swift

After reading that functions are just a special case of closures, being a named constant closure, is there any reason to use a function over a closure? I ran into a problem recently where I needed to pass my functions into a method for sequential animation and ended up needing to transform my functions into closures. So with that disadvantage and loss of flexibility, why would I ever want to use functions over closures?
I advise you to use closures whenever you need to use this code just once and only in that exactly point where you implement it in your whole code.
If you need to reuse the logic of a block of code, the best way is to encapsulate your code inside a function and use it as a callback.
I can´t imagine a situation when you would need to convert all your functions into closures. All you need to do is create a function that fits with your closure signature.

Function Pointer

How is that function pointer better than if-else or switch case? Is it because function pointer helps callback functions and thus promotes asynchronous implementation?
It's better if the functions are not known beforehand. How would you design the C standard library qsort() without using a function pointer?
It's not "better", it's different. Yes, the main purpose of function pointer is to provide callback functionality. No, it is not directly related to asynchronism.
Check this article for more information on function pointers.
According to Nigel Jones, it improves code readability, among others
http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/
He even provides some examples:
http://www.rmbconsulting.us/Publications/jump-Tables.pdf
Function pointers result in a better design of your code. Consider when you have an option of calling one function out of fifty functions. How massive would the switch case be? However, you could easily map all the function pointers as per their id, and call the appropriate function from the map using the "id". The code would be easily maintained and look neat.
There are other benefits and powers of using function pointer.
For a good explanation, look at the tutorial here

Function pointer and normal function

What are the differences between normal function and function pointer.
One which I know is in case you are using a library in your software stack which
gives you only function pointers then you can fill in the pointers for
use later.
Calling a function through a function pointer means the call cannot be inlined; in certain cases, this can result in quite a performance penalty. (For example, C's qsort() going through a function pointer for each compare, vs. C++'s sort() being able to inline the comparison.)
Declaring a function pointer requires a non-trivial syntax that is not as commonly used as other parts of the language, resulting in a "mental speed-bump" for most when reading the source. It is usually typedef'ed for that reason.
A function pointer is just a pointer to a normal function. It can be passed around like any other pointer, and can be invoked somewhere other than where it was created.