You must have used the word ‘function’ a number of times. In general usage, it means to operate in a particular way. For example, the function of a pencil is to write. This function is specific and clearly defined.
Let’s learn what functions broadly represent.
Hi. We covered conditional statements and loops in the previous lectures. You also wrote the code and saw how lengthy it became at several stages. If you are required to use the same loops again and again in the program, should you write the entire thing again? In this session, you will learn how to use functions to make your code more efficient. Let's take the example of a company with different departments. There is a head and there are multiple teams. You have production, you have sales, you have service, et cetera. Once production is done, the head of the department calls the sales head and tells him to begin sales. Each time a product is made, the sales team is called in to sell it. The function of the sales team is to sell the product. You aren't concerned about how they will go about selling the product. They have their own internal rules and standards that people outside the sales team are not concerned about. Similarly, in computer science, there are pieces of code called functions.
You aren't always interested in knowing how they go about getting you the required results. You simply use the anytime and anywhere you wish to get the desired results. Now, let's look at this in detail. But first think about how a function should be. It should have some name, as the term function suggests it should do something. It should be called that is, it should show up only when you want it and where you want it to.
Functions in programming can make code more efficient.
Functions are like pieces of code that can be reused multiple times in a program.
The example given is a company with different departments and teams that have their own functions to perform.
A function should have a name, perform a specific task, and be called only when needed.
Using functions in programming can make code easier to read and maintain.
So, a function basically has a defined name and needs to be called when an action is required. Now, let’s look at functions in Java.
Note: You can download the following Java files to practise the codes while watching the video. Right-click on the Java files and open them with IntelliJ. Please be careful with the name of each file. The name should be exactly the same as the class name inside it, for the code to run. So make the changes accordingly.
You saw how functions are defined in Java. Defining a function is important as it determines what the function will be able to do. When you use ‘void’ in the function definition, you cannot use the function to return a value.
When you wish to return a value, you define its data type in the function definition.
Let’s now look at some of the important considerations you need to take into account when creating functions that return values.
Note: You can download the following Java files to practise the codes while watching the video below:
Till now, the functions that you saw could print a statement or return a value that could be used in some other operation. But these functions were not interacting with the code. You will want your function to take values from the code itself: e.g. a user input. In the next lecture, you will learn how to modify your function definition to make it interact with the code.