What Is a Function?

In my short mentoring career, I have very often seen people struggle with understanding the most basic concepts, such as a variable or a function. Unfortunately, a definition does not always help. I will try to take a slightly broader look at what is hidden behind the word function.

Newton's Cradle Balls

From this article you will learn:

  • What is a function and what is it used for?
  • What is the difference between declaring and calling a function?
  • How to declare and call functions?
  • What happens if we call a function that does not exist?

Function is a word that comes from Latin and means nothing other than "performing". Actually, this is exactly where we should stop for a moment and think about that translation. Both in every programming language and in the real world, the word function means the same thing. For example: a television has the function of displaying an image, a calculator has the function of counting, and living organisms have life functions, such as breathing.

Logical, right?

Declaration and Calling

However, simply having a function is not always directly connected with performing it. Notice that a television will not display an image by itself, a calculator will not count by itself, and a human will not breathe by themselves either, even though it may seem to us that this is exactly what happens. There is always some impulse that causes a given function to start working.

In a television, the remote control is responsible for starting the display function. Depending on other conditions being met, it starts the desired functionality. What kind of conditions can these be? For example, a constant power supply, pointing the remote in the right direction, or the construction of the television itself. It is similar with humans. A person breathes, but the lungs, bronchi, or diaphragm would not function without the right impulse from the brain. Do you see the dependency?

The mere existence of a function does not mean that it works and does what it was created to do.

The same rules exist in programming.

Let us look at the following example written in JavaScript:

js
function showWarning () {
  // here we will put the code responsible for displaying a warning
}

As you can see, the function I wrote is called showWarning and, in principle, it will display warning information. The function has been created, but it does nothing for two reasons:

  • the function has not been implemented yet - in other words, we still need to write how we are going to display the warning,
  • the function has not been called yet.

Calling Existing Functions

If we want to call an existing function, it is enough to provide its name and add () at the end. Inside these parentheses, we can pass parameters to our function, which means we can tell it what data it should be called with. It is like a paper shredder that, depending on the thickness of the paper and the selected shredding precision, will use different blades.

js
showWarning();

Our function exists and has been called without parameters, so it works. Except it still does not do anything. Let us add a little bit of code, then.

The functions we can use while creating software are not only the ones we wrote ourselves. There is a whole range of functions delivered together with the language that we can use. We only need to know they exist. For example, we can get that knowledge from the documentation and call them. Let us use the alert function. This function will let us display an appropriate message to the user. We will pass that message to it using the first parameter of the function. Let us modify our showWarning function accordingly.

js
function showWarning () {
  alert('This is a warning');
}

Now, as soon as we call our function, the message we passed in the first parameter will be displayed. For our function to be genuinely useful, it should also be reusable. We can expect that many times in our program we will want to display different warnings somewhere. When declaring a function, we can name the parameters that we will pass to it. Our showWarning function should definitely know what the content of the message we want to display will be. Let us name this parameter message.

If we want to extend it a bit more, we can add a second argument that will describe the importance of our message. Let us modify the function so that the text is written in uppercase if the function is called with a second argument equal to true. Let us name this argument isImportantMessage. Let us do it.

js
function showWarning (message, isImportantMessage) {
	const messageToShow = isImportantMessage ? message.toUpperCase() : message;
	alert(messageToShow);
}

showWarning('This is an important message', true);
showWarning('This message is not that important');

In the example above, I allowed myself to write two calls to our function right away. Notice that the first parameter we pass to the function is always the content of the message. When JavaScript executes our code, it will assign the values we passed to the corresponding arguments. So, in the first call, message is 'This is an important message', and in the second one it is 'This message is not that important'.

Calling Functions That Do Not Exist

Here the matter is quite simple - you cannot call a function that does not exist. It is like someone telling you to breathe underwater. You probably do not have that ability. If I am right, during the attempt your body will rebel and it may end very badly for you. JavaScript works the same way. If you try to call a function that is neither in the documentation nor prepared by you, then your program will stop working, and the browser console will log a red message: Uncaught ReferenceError: function is not defined.

The code cannot work because it simply does not know what we meant.

So avoid typos, check the console, and always think about whether you can call a given function.

Summary

The topic of functions is very broad, and I will definitely return to it in future articles. I hope that my explanations, supported by examples taken from reality, will help untangle what functions are and how their declaration differs from their call.

Share this article:

Comments (0)

    No one has posted anything yet, but that means.... you may be the first.

You may be interested in

If this article interested you, check out other materials related to it thematically. Below you will find articles and podcast episodes authored by me, as well as books I recommend that expand on this topic.

Moduły w JS by
Article
2023-01-15

Modules in JS

It is hard to imagine an application made of a single file. It is just as hard to imagine developing such code and maintaining it later. Fortunately, JavaScript supports modules, but it was not always like that.

Read more
The True Story of JSON by Aleksandr Korchagin
Article
2022-02-10

The True Story of JSON

HRejterzy once blamed JSON for a project problem in one of their videos. If we assume that JSON is responsible for so many errors in IT projects, maybe it is worth getting to know it better. Meet the true story of JSON.

Read more

Zapisz się do newslettera

Bądź na bieżąco z nowymi materiałami, ćwiczeniami i ciekawostkami ze świata IT. Dołącz do mnie.