+5 votes
233 views
in Web development by (242k points)
reopened
Callback: what are callback functions?

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

What is a callback?
How does a callback work?
Callback function in JavaScript
Callback function in PHP
Callback functions in Python
Callback functions in Java
Callback functions in C

image

Callback: what are callback functions?

JavaScript is one of the most popular languages ​​for creating websites. The possibilities offered by this scripting language are exploited to design interactive pages that are capable of reacting to requests . For this, variables, objects and functions are used within the framework of the language. All of them can fall back on each other and they always reflect the same result in different browsers. Behind most buttons or content screens that appear at any given time on websites is a callback function ..

However, functions of this type are not unique to JavaScript: other popular programming languages, such as C, Java, PHP, or Python, also use the callback to pass certain user parameters in the easiest way.

Index
  1. What is a callback?
  2. How does a callback work?
    1. Callback function in JavaScript
    2. Callback function in PHP
    3. Callback functions in Python
    4. Callback functions in Java
    5. Callback functions in C

What is a callback?

Functions always refer to certain parameters. If you assign a function as a parameter to another function , we will talk about a callback . Callback functions are widely used in libraries and frameworks , such as jQuery, Angular, or node.js JavaScript applications. These applications are suitable for creating extensible functions and run only after another event or circumstance occurs..

How does a callback work?

The callback function always has a certain effect that is related to certain circumstances . In other words, it is only invoked if another clearly defined action has taken place. A good example of a callback function is event handlers, which are used, for example, in HTML elements such as buttons. The event could be a mouse click that causes the callback to execute , and the function itself could cause a redirect to another page or pass a value that was entered in a form.

The main difference between a normal function and a callback would be the following: while a normal function executes directly, the callback function is only defined and called and executed only when a specific event occurs. As we have mentioned, callbacks are used in many programming languages, and although the syntax and structure of the methods differ, their principles are maintained in all languages..

Callback function in JavaScript

Callback functions are widely used in JavaScript programming. This could be, for example, a function that is executed when a button is pressed or some content related to a setTimeout () function , which causes a time delay.

A simple example of a JavaScript callback would be an ordinary button:

  document.getElementById("Button1").addEventListener("click", function() { console.log("Se ha pulsado el botón"); }, false);  

In this example, an EventListener is used as a callback that fires when a certain event occurs; in this case, click on the button identified as? Button1 ?. As soon as the button is clicked and the conditions for the callback function are met, the callback is executed and the terminal issues the message? Button has been pressed ?. To ensure compatibility with older versions of the browser, the example also uses the parameter? False ?.

Callback function in PHP

In PHP, callback functions are executed similar to JavaScript. However, in this scripting language, callbacks can also be methods of an object , including static class methods.

This would be an example of a classic callback function in PHP:

  function my_callback_function() { echo '¡Hola, mundo!'; }  

Calling this function generates the string ? Hello world!?. To execute the callback as a method , proceed as follows:

  class MyClass { static function myCallbackMethod() { echo '¡Hola, mundo!'; } }  

In PHP, callbacks are a widely used method for functions to communicate with each other . They are usually used to implement plugins or modules in a clean way and to guarantee their functionality.

Callback functions in Python

Python is one of the most popular programming languages ​​due to its simple syntax. Thanks to the versatility it offers, both in software and hardware , this language is perfect to start programming. It is used especially in the fields of data science and machine learning and has many advantages over other languages.

In Python, the callback is mainly used to assign different events to user interface elements . In the following example, two functions are defined, that is, the callback ? Get_square? and the call? caller ?. Finally, the callback is executed :

  >>> def get_square(val): return val ** 2 >>> def caller(func, val): return func(val) >>> caller(get_square, 5)  

The syntax of the callback function is very simple, similar to that of JavaScript and PHP .

Callback functions in Java

It is quite unusual to execute a callback function in Java. Technically, it can be done, but it requires the reflection function . With this function methods and functions that are processed in objects are established. Consequently, the reflection function is more of a library function than a callback .

The SAX principle also works in a similar way to the callback function in Java. In this case, the SAX parser reads an XML file and calls some specific callback method based on the incoming event. These include the startDocument () and startElement () methods.

Callback functions in C

In the C programming language, callbacks can be built in similar to JavaScript. Commonly used to program systems and applications, this language is an integral part of kernel programming for systems and operating systems. Many programming languages, such as JavaScript, PHP, C ++, Java, or C #, rely heavily on C syntax and properties. Therefore, the syntax and methods of the different languages ​​also coincide in many respects.

This would be an example of a callback function in C:

  void A() { printf("Soy una función A\n"); } // La función callback void B(void (*ptr)()) { (*ptr) (); // Aquí se invoca el callback de A } int main() { void (*ptr)() = &A; // Se ejecuta la función B // La función A se pasa como argumento B(ptr); return 0; }  

Consequently, the output of this code is:? I am a function A ?. As in JavaScript, callbacks always run when a certain event occurs. In C, these functions are used to create a number of new libraries for further development of programs and to generate signals from the kernel needed when handling asynchronous events.


...