Java Tutorial. Functions

What is a function and how are they used?

Emmanuel Tejeda
Developers Yesterday

--

Photo by Saad Ahmad on Unsplash

In Java, and almost all programming languages, we use functions to make our code neater, more compact and easier to manage. In this tutorial I will show you how functions accomplish these three goals.

What is a function

First let’s define what a function is. A function is a block of code that performs a specific task. They can be reused as many times as needed and they are normally used to pass information and receive information back. Let’s give an example of a problem, and how we would solve this problem using functions. Keynote, in java, functions are called methods, but to make this guide relevant to other languages, I will use the word functions instead.

Let's say I have a client. The client wants me to calculate the cubed root of all the numbers he has. So, if my client gives me the number 5, the code should return 125. Easy right?

As you can see, we just take the clients number, and multiply it by itself, three times. But what if our client gives us multiple numbers at once.

Well, you might say the simplest solution would be to type in that formula three times, right?

--

--

Emmanuel Tejeda
Developers Yesterday

My name is Emmanuel Tejeda. I am currently a software developer with a passion for politics, community, and History.