In Java we have a facility of dividing a given task into various modules. Then we write a function for every module. In the end the modules are called into the main function as per the requirements. Functions are useful in the following manner:1. It avoids repetition of tasks. For example, we can write a function which can calculate the area of a square. Now for any value we require, we will simply need to pass the value as a parameter to the function, and we will get the area of the square as the output. Thus for every value we do not need to write a separate program.2. Making changes to a modular program is easier. For example if we have a function which computes the tax on a given income. When the income tax laws change we will only need to change the values in the given function. This is much easier than making changes in a program without modules.Function HeaderThe function header is at the beginning of a function. It defines certain important parameters about a function.Syntax:access_specifier static/non static return type (parameter1, parameter 2, … parameter n)Explanation:1. The access specifier defines who can access the function. There are three types of access specifiers in Java for any object or function.a. Public: Any class or object can access this function.b. Protected: This function can be accessed only by the class in which it is defined and it’s derived classes.c. Private: This function can be accessed only by the class in which it is defined.2. Static/ Non Static Function: A static function can be called without creating any instance of the class. A non-static function can be called only by creating an instance of the class in which it is defined. We will see function calling later in this article.3. Return Type: The function can either return a value, or not return a value. If it returns a value, the data type of the value is mentioned here, like int, char, boolean etc. If the function does not return a value, the data type used is void.4. Parameters: A function may accept zero or more parameters. Using these parameters the function might do the computations which give certain outputs. Some functions may not accept any parameters for their computation.Example 1:class FC{public static void square(int z){z=z*z;System.out.println(z);}public static void main(String args){int a = 10;square(a);}}Output:100Explanation:In this program we have a function called square. From the header of the function, we can understand four things:1. This is a public function.2. The function is static. So it can be called without creating an instance of the class.3. The function does not return any value, hence the return type void.4. The function accepts one parameter, an integer value which it stores in z.To call a static function in the same class, we have to simply write the function name followed by parenthesis. The required number of parameters can be entered into the parenthesis. In this case it was square(a);. This is an example of static, non-returning function.Example 2:package javaapplication4;public class JavaApplication4{public void square(int z){z = z*z;System.out.println(z);}public static void main(String args){int a =5;JavaApplication4 j = new JavaApplication4();j.square(a);}}Output:25Explanation:In this example, we have a class JavaApplication4, in which there is a main function and a non-static function square. In case of non-static functions, we do not need to mention the word static. There is nothing mentioned. To call a non-static function we need to create an instance of the class in which it was defined. So we create an instance of the class using the object j, and then call the function as j.square(a);. This is an example of non-static, non-returning function.Example 3:package javaapplication4;public class JavaApplication4{public static int square(int z){z = z*z;return z;}public static void main(String args){int a =5;int b =square(a);System.out.println(b);}}Output:25Explanation:This is a returning static function. One thing to note, whenever we are calling a returning function is that we need to specify where the returned value will be stored. In this case, we have written the function call as int b = square(a), thus mentioning that any value returned by the function square should be saved in variable b. It is mandatory that the data type of the variable being used to save the value returned by the function, should be the same as the return type of the function, in this case both being integers.


I'm Niki!

Would you like to get a custom essay? How about receiving a customized one?

Check it out