Functions In JAVA programming Hands on Coding

Topic

  1. Function
  2. Syntax of function
  3. Types of function
  4. Function without return type and without parameter
  5. Function without return type and with parameter
  6. Function with return type and without parameter
  7. Function with return type and with parameter
  8. Call by Value
  9. Passing Array to a function
  10. Recursion



Function

  • Functions are the collection of statements which performs a perticular task.
  • It will be executed when it is called by its name.
  • A large program is devided into small building blocks for simplicity and this building block itself is function.
  • We can call a function many times using its name.
  • The important features of function is "code reusability".
  • The JAVA library provides many pre-defined functions.

Syntax of function?

Access Specifier

  • This is a keyword which can be used to provide accessibility for the function.
  • There are three type of access specifiers in java, named as, public, private and protected.

Return Type:

  • This indicates type of value which will be returning inside specific function.
  • If we don't want to return any value then, we can use void keyword in place of return_type.

Function Name:

  • This is the actual name of the function which will be created by the user.
  • This is also used at the time of calling the function.

Parameter List:

  • This is the place, where we can pass parameters or variables.
  • The value of parameter is passed by calling the function
  • This is optional part.
  • It is also called command line argument.

Body

  • Inside body we write the actual code, to perform the perticular task.

Sample

 //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 and 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, and x ,y are the parameters whose 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 different types of function in java.

  1. Predefined Function
  2. Userdefined of function

Predefined Function

  • The function which is already defined in the library is called predefined function.
  • Example, println, nextInt, nextFloat etc.

Userdefined Function

  • The function which will  be created 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 without return type and without parameter

As you can see from the above syntat in place of return type void(no return type) keyword is used and no parameters in the place of parameter list. For better understanding see the below sample code.

 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 without return type and with parameter



As you can see from the above syntax in place of return type void(no return type) keyword is used and there are two parameters a and b in the place of parameter list. For better understanding see the below sample code.

 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 without parameter

As you can see from the above syntax in place of return type int( return integer type) keyword is used and there is no parameter in the place of parameter list. For better understanding see the below sample code.

 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 parameter


 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  
 */  

From the above syntax 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.


Call by value:

In this type of calling of function directly value is passed at the time of calling a function. For better understanding see the sample 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 the elements in an 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  
 */  


Post a Comment

0 Comments