In the upcoming session, you will learn about something called functions.
Let us try to understand what functions are and how will they help us.
In the next session you're going to take a look at a very interesting concept in Java which we call as functions. So basically, going by the name functions itself, a function does nothing but perform some certain set of actions as soon as you call that function. So why would you need to use functions in the first place? Now imagine that we wrote a code like this. So this code basically prints the list of all the even numbers between zero till 100. Now, if you wish to reuse this code somewhere else in the program, well, how would you do that? Given our current knowledge, we have no other way but to write this entire piece of code again. Suppose I write this piece of code here and it prints down the list of all even numbers. Now if I again need to print the list of even numbers somewhere else in the program, then I would need to write all these four lines again. Is there a clever and more efficient way to do the same job? Well, here is where functions come into play. So basically functions are like this black box and inside the black box you have some certain actions which are getting performed. You call this function, you give it some name function name. And when you want these several actions to get executed, you simply call this function. So basically, if I want these three four statements or these three four actions to get executed whenever I want, I can enclose this entire thing within the box of what I call as the function. I have to give a name to this function. Suppose this function is called as Print Even. So in this case I simply need to write print even when whenever I want this entire box of actions to execute. So function is basically nothing, but it is a bigger box containing a list of actions that function or that box has a name. And whenever you want those several actions to get executed you simply call that function or you write that function in your program.
Functions in Java perform a certain set of actions when called
Using functions allows for reusability of code
Functions are like a black box containing actions that can be executed by calling the function
Giving a name to the function allows for easy execution of the actions within the box
Functions are a more efficient way of executing repeated actions in a program.
You learned that a peice of code to be re-used multiple times in a program can be written as something called a "function" and can be then re-used multiple times.
Consider you have a browser window open, whenever you want to open a new tab you would just click on a button without worrying how a new tab opens.
Functions in Java provide you similar facility to do tasks without worrying about their implementation. You can also write your own functions in Java.
Let us now take a look at some useful functions in Java.
You learned about the length() and charAt() functions in Java which tell you the length of the string and give you a character at a particular index respectively.
Let us take a look at another interesting function in Java.
You learned about the length() function which will give you the length of an array.
Java provides you with multiple such functions which make your life easier. Also, you can write your own functions.
In the next session, you will learn more about functions and how they help you in writing better computer programs.