Now, there are many articles and videos explaining callbacks, most of them great. But it takes a great deal to understand them. I believe this is because they are written by programmers for programmers… and that makes sense. But what if you are a programmer and still are not getting it?
Okay, so, let me give it a try:
The key thing is that you need to understand that there is nothing really new here. Well, there maybe is, but it is easy to understand: a function can be passed to another function as a parameter.
We good? Okay, let’s keep going.
We said that ‘hello()’ is just a pointer to a location in memory with some code. Same as if intestad of a piece of code it is a piece of data. Let say you declare a variable like ‘var a = 2;’. It is doing the same: putting somewhere in the memory the value “2”, and assiging to the name “a” the memory location where the value is. So when you need to retrieve it again, you just need to call ‘a’
The only difference is that when you declare a function, you are telling the machine (preprocessor, compiler or whatever it it) that it is going to find a piece of code (a function) or a piece of data.
So let’s say we have
1010: goodbye ()
The same way you pass a variable, you can pass a function because it is just a location in the memory. Therefore you can do things like hello(a, goodbye), passing a variable and a function.
Hope this was useful!