Catch us on

Friday, April 1, 2016

Function in java

Functions in java or any other language helps to reduce the lines of codes in our program.
Suppose we have to perform a particular task repeatedly but not continuous repetition like loops then instead of writing those line of code we used to make a function with some name just need to call it at the time of our need.
      Function has following  parts:-
·         Name : As the name suggests, the function should have proper name.
·         Definition : definition means function should have those statements that are performing the required task.
·         Return type: return type says that what type of value a function returns whether it is integer, character etc.
NOTE: Return type of a function could be void which means it does not return nay value.
·         Arguments :the parameters on which the task is to be performed.
NOTE: it is not necessary for a function to have a arguments.
Syntax :
           return_type function_name(arguments)
                  {
                    // statements
               }
Example:
1.       class Function {
2.       void display (int n)        //CREATION OF FUNCTION                                                       
3.       {
4.       System.out.println("Function with arguments and without any return statement");
5.       }
6.       int show()
7.       {
8.       System.out.println("Function without arguments but with return value");
9.       return 10;
10.   }
11.   public static void main(String [] args)
12.   {
13.   Function fn=new Function();
14.   int rtrn;
15.   fn.display(10); //CALLING FUNCTION display()
16.   rtrn=fn.show();// CALLING FUNCITON show()
17.   }
18.   }
On running the above program we get the output as:


Important points to remember regarding function:

  1.            A function may or may not return any value.
  2.           A function may or may not have arguments.
  3.          It is not necessary that function is in same class in which it is called.
  4.           A function without any argument may return a value and vice versa.

THANK YOU FOR READING THIS ARTICLE.
PLEASE SHARE IT ON FACEBOOK,TWITTER AND OTHER SOCIAL NETWORKS AND RECOMMEND YOUR FRIENDS TOO.
Kishan Agarwal



Please Share


No comments:

Post a Comment