What are callback functions?

JavaScript is one of the main languages used for creating websites. This scripting language makes it possible to design websites to be interactive 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 JavaScript. Other well-known programming languages (e.g. C, Java, PHP, and Python) also use callbacks to easily pass specific user parameters.

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

Looking for an alternative to the traditional .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 parameters 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 frameworks, such as the JavaScript applications jQuery, Angular, and Node.js. They are designed to extend functions and be called back for specific events or circumstances.

How does a callback function work?

A callback function always has a specific action which is bound to a specific circumstance. 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 difference between a normal function and a callback function can be summarised 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 previously mentioned, callback functions are used in a variety of programming 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 JavaScript

Callback functions are frequently used in JavaScript programming. These can be functions that are called when a button is clicked or content that is set on a time delay with the setTimeout() function.

A simple example of a callback function in JavaScript 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, fulfilling the conditions 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 compatibility with older browsers.

Callback functions in PHP

In PHP, callback functions are called in a similar way to JavaScript. 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 communicate 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 programming languages due to its simple syntax. Its versatile use in software and hardware makes it the perfect programming language for beginners. This language is in high demand and offers many advantages over other languages, especially 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 JavaScript and PHP.

Callback functions in Java

It is rather unusual to execute a callback function in Java. Technically, it can be done, but it requires the reflection feature. This feature inspects methods and functions and processes them into objects. The reflection 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. startDocument(), startElement(), etc.) is called based on the received event.

Callback functions in C

In the programming language C, callback functions can be integrated in a similar way to JavaScript. This language is often used for system and application programming and is a core component for programming system kernels and operating systems. Many programming languages (e.g. JavaScript, PHP, C++, Java, and C#) are heavily based on the syntax and properties of C. Therefore, there are also many similarities 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 JavaScript, a callback function is called whenever a specific event has occurred. In C, these functions are used to create various new libraries for future programming and to output signals from the kernel that are required for handling asynchronous events.

Managed WordPress Hosting
Create your site with AI, we manage the rest
  • Stress-free, no matter your skill level with easy AI tools
  • Full customisation with themes and plugins
  • Hassle-free updates and less admin
Was this article helpful?
Page top