JavaS­cript is one of the main languages used for creating websites. This scripting language makes it possible to design websites to be in­ter­act­ive and able to react to input. The language uses variables, objects, and functions to this end. These can all interact with one another and always display the same result no matter which browser is used. Most buttons or displayed content use a callback function at some point.

However, this type of function is by no means a unique selling point for JavaS­cript. Other well-known pro­gram­ming languages (e.g. C, Java, PHP, and Python) also use callbacks to easily pass specific user para­met­ers.

Your very own .uk domain name!
Short, sharp, .uk

Looking for an al­tern­at­ive to the tra­di­tion­al .co.uk extension, or want to grow your online presence? Give .uk a try today.

£1 for 1 year!

What is a callback function?

Functions always work with para­met­ers which they refer to. If you pass another function to a function as a parameter, it is called a callback function. These callback functions are often used in libraries and frame­works, such as the JavaS­cript ap­plic­a­tions jQuery, Angular, and Node.js. They are designed to extend functions and be called back for specific events or cir­cum­stances.

How does a callback function work?

A callback function always has a specific action which is bound to a specific cir­cum­stance. Therefore, a callback function is only called once a clearly defined operation has been performed. Event handlers are a good example of a type of callback function. These are used in HTML elements such as buttons. An event can be a mouse click that causes a callback function to be executed. This callback function will probably redirect the user to another page. It could also be used to send a value in a form being filled out.

The main dif­fer­ence between a normal function and a callback function can be sum­mar­ised as follows: A normal function is called directly, while a callback function is initially only defined. The function is only called and executed once a specific event has occurred. As pre­vi­ously mentioned, callback functions are used in a variety of pro­gram­ming languages. While the syntax and structure of the methods may differ, the basic idea behind callbacks remains the same across all the languages.

Callback functions in JavaS­cript

Callback functions are fre­quently used in JavaS­cript pro­gram­ming. These can be functions that are called when a button is clicked or content that is set on a time delay with the set­Timeout() function.

A simple example of a callback function in JavaS­cript is an ordinary button:

document.getElementById("Button1").addEventListener("click", function() {
console.log("Button was clicked");
}, false);

In this example, an event listener was used as a callback which is executed when a specific event occurs - in this case, the event is clicking the button with the ID “Button1”. Once the button has been clicked, ful­filling the con­di­tions for the callback function, the function will be executed and the message “Button was clicked” will be displayed in the console. In this example, the parameter “false” was also used to ensure backward com­pat­ib­il­ity with older browsers.

Callback functions in PHP

In PHP, callback functions are called in a similar way to JavaS­cript. However, in this scripting language, callback functions can also be methods for an object, including static class methods.

The following is an example of a classic PHP callback function:

function my_callback_function() {
	echo 'Hello world!';
}

When you call this function, the string “Hello world!” is returned. To call the callback as a method, proceed as follows:

class MyClass {
	static function myCallbackMethod() {
		echo 'Hello world!';
	}
}

In PHP, callbacks are a popular way to have functions com­mu­nic­ate with each other. They are often used to smoothly implement plug-ins or modules and to ensure that they work.

Callback functions in Python

Python is one of the most popular pro­gram­ming languages due to its simple syntax. Its versatile use in software and hardware makes it the perfect pro­gram­ming language for beginners. This language is in high demand and offers many ad­vant­ages over other languages, es­pe­cially in data science and machine learning.

In Python, callbacks are mainly used to assign various events to UI elements. In the following example, two functions are defined: the callback “get_square” and the call “caller”. The callback is then called:

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

The syntax in the callback function is very simple, just like in JavaS­cript and PHP.

Callback functions in Java

It is rather unusual to execute a callback function in Java. Tech­nic­ally, it can be done, but it requires the re­flec­tion feature. This feature inspects methods and functions and processes them into objects. The re­flec­tion feature is thus more a library function than a callback.

The SAX principle also works in a similar way to the callback function in Java. Here, an XML file is read by the SAX parser, and a specific callback method (e.g. startDoc­u­ment(), star­tEle­ment(), etc.) is called based on the received event.

Callback functions in C

In the pro­gram­ming language C, callback functions can be in­teg­rated in a similar way to JavaS­cript. This language is often used for system and ap­plic­a­tion pro­gram­ming and is a core component for pro­gram­ming system kernels and operating systems. Many pro­gram­ming languages (e.g. JavaS­cript, PHP, C++, Java, and C#) are heavily based on the syntax and prop­er­ties of C. Therefore, there are also many sim­il­ar­it­ies in the syntax and methods of the different languages.

The following is an example of a callback function in C:

void A()
{
	printf("I am function A\n");
}
// The callback function
void B(void (*ptr)())
{
	(*ptr) (); // Here the callback to A is called
}
int main()
{
	void (*ptr)() = &A;
	// Function B is called
	// Function A is passed as an argument
	B(ptr);
	return 0;
}

The output of this code is thus the following: “I am function A”. Just like in JavaS­cript, a callback function is called whenever a specific event has occurred. In C, these functions are used to create various new libraries for future pro­gram­ming and to output signals from the kernel that are required for handling asyn­chron­ous events.

Managed Hosting for WordPress
Create your site with AI, we manage the rest
  • Stress-free, no matter your skill level with easy AI tools
  • Full cus­tom­isa­tion with themes and plugins
  • Hassle-free updates and less admin
Go to Main Menu