Translate

Function in Java

Topic

Function

  • It is a collection of statement that performs an specific task.
  • It executes when it is called by its name.
  • A large program is devided into a number of small building block for simplicity and this building block is called function.
  • We can call a function again and again.
  • The most importent features of function is code reusability.
  • The JAVA library provides many pre-defined functions.

Syntax of function?

 Access Specifier
  • It is a keyword which is used to provide accessibility of function.
  • There are three access specifier are used in java public,private and protected.
Return Type
  • It is such type of element which indicates that which type of value is returning by this function.
  • If we do not want to return any value then we use void keyword in place of return_type.
Function Name
  • It is the actual name of the function.
  • It is also used at the time of calling the function.
Parameter List
  • It is the place where we can pass a number of parameter/variable.
  • These variables may be used in the program.
  • The value of parameter is passed from the calling of function.
  • It is optional part.
  • It is also called command line argument.
Body
  • It is the place where the actual code is written to perform the specific task.
Sample 1
//function without parameter
public void add()
{
int x,y=20,z=30;
x=y+z;
System.out.println("Add="+x);
}
  • Here public is access specifier,void is return type,add is function name.
Sample 2
//function with parameter
public void add(int y,int z)
{
int x;
x=y+z;
System.out.println("Add="+x);
}
  • Here public is access specifier,void is return type,add is function name,y and x are the parameters which value is passed at the time of calling.
Example
class EASY
 {
  //function declaration
  public void add()
   {
    int x,y=10,z=30;
    x=y+z;
    System.out.println("Add="+x);
   }
  public static void main(String[] args) 
  {//creating object
   EASY obj=new EASY();
   //calling function
   obj.add();
  }
 }
/*
### OUTPUT ###
Add=40
*/

Types of Function

There are two types of function in java.
  • Predefined Function
  • Userdefined of function
Predefined Function
  • The function which is predefined in the library is called predefined function.
  • Example of predefined function are print,println,nextInt,nextFloat etc.
Userdefined Function
  • The function which is made by the user is call userdefined function.
  • add,sub,multiply,div etc userdefined name.
There are four more category of userdefined function in java.

Function with no return type and no parameter

  • As you can see from the above syntaxt in place of return type void(no return type) keyword is used and there is no parameter in the place of parameter list.For better understanding see the below example.
class EASY
 {
  //no return type no parameter
  public void add()
   {
    int x,y=10,z=30;
    x=y+z;
    System.out.println("Add="+x);
   }
  public static void main(String[] args) 
  {//creating object
   EASY obj=new EASY();
   //calling function
   obj.add();
  }
 }
/*
### OUTPUT ###
Add=40
*/

Function with no return type and with parameter

  • As you can see from the above syntaxt in place of return type void(no return type) keyword is used and there are two parameter(a and b) in the place of parameter list.For better understanding see the below example.
class EASY
 {
  //no return type with parameter
  public void add(int y,int z)
   {
    int x;
    x=y+z;
    System.out.println("Add="+x);
   }
  public static void main(String[] args) 
  {//creating object
   EASY obj=new EASY();
   //calling function
   obj.add(10,20);
  }
 }
/*
### OUTPUT ###
Add=30
*/
  • Here 10 will assign to variable y and 20 will assign to variable z.

Function with return type and no parameter

  • As you can see from the above syntaxt in place of return type int( return integer type) keyword is used and there is noparameter in the place of parameter list.For better understanding see the below example.
class EASY
 {
  // return type with no parameter
  public int add()
   {
    int x,y=10,z=20;
    x=y+z;
    return x;
   }
  public static void main(String[] args) 
  {//creating object
   EASY obj=new EASY();
   //assigning the returning value to rs
   int rs=obj.add();
   System.out.println("Add="+rs);
  }
 }
/*
### OUTPUT ###
Add=30
*/

Function with return type and with parameter

  • As you can see from the above syntaxt in place of return type int( return integer type) keyword is used and there are two parameter(a and b) in the place of parameter list.For better understanding see the below example.
class EASY
 {
  // return type and with parameter
  public int add(int y,int z)
   {
    int x;
    x=y+z;
    return x;
   }
  public static void main(String[] args) 
  {//creating object
   EASY obj=new EASY();
   //assigning the returning value to rs
   int rs=obj.add(10,20);
   System.out.println("Add="+rs);
  }
 }
/*
### OUTPUT ###
Add=30
*/

Call by value

  • In this type of calling of function directly value is passed at the time of calling.For better understanding see the example below
class EASY
 {
  public void add(int y,int z)
   {
    int x;
    x=y+z;
   System.out.println("Add="+x);
   }
  public static void main(String[] args) 
  {//creating object
   EASY obj=new EASY();
   //callingof function
   obj.add(10,20);//call by value
  }
 }
/*
### OUTPUT ###
Add=30
*/

Passing array to function

//find the sum of all the element of array
class EASY
 {
  //using array as parameter
  public void array(int ar[])
   {
    int sum=0;
    for(int i=0;i<ar.length;i++)
     sum=sum+ar[i];
     System.out.println("Total sum="+sum);
   }
  public static void main(String[] args) 
  {
   int b[]={10,50,40,80,70};
   //creating object
   EASY obj=new EASY();
   //calling of function
   obj.array(b);//passing array to function
  }
 }
/*
### OUTPUT ###
Total sum=250
*/
  • Here in the above example all the elements of array b will be passed to array ar.

Recursion

  • The process of calling a function by itself is called recursion and the function that calls itself is called Recursive function.
 Example 1
class EASY
 {
  //using array as parameter
  public void demo()
   {
     System.out.println("Hello");
     demo();//self calling
   }
  public static void main(String[] args) 
  {
   //creating object
   EASY obj=new EASY();
   //calling of function
   obj.demo();
  }
 }
/*
### OUTPUT ###
Hello
Hello
Hello
Hello
-----
-----
-----
infinite time Hello
*/
Example 2:Table of 5 using recursion
//table of 5
//5 10 15 20 25 30 . . . .50
class EASY
 {
  //using array as parameter
  public void table(int no)
   {
     if(no<=50)
     {
     System.out.print(no+" ");
     no=no+5;//increment by 5
     table(no);//self calling
     }
   }
  public static void main(String[] args) 
  {
   //creating object
   EASY obj=new EASY();
   //calling of function
   obj.table(5);
  }
 }
/*
### OUTPUT ###
5 10 15 20 25 30 35 40 45 50
*/
Next Post Previous Post
No Comment
Add Comment
comment url