JAVA

Functions In Java With Examples

Function in java
Function in java

Java Function

Functions in java
Functions in java

A function is a process to divide a program into a different module and each module contains sets of instructions. It is basically used to reuse the programming instructions and execute with module identifier. The functions are declared inside the class and execute with the help of objects. We create an object of the class to access the data member. The data and member function declare within the class and execute its related instructions and generate output.

Return Type Function

The return type is a type of function that declares with the data types and returns a value after processing and can use outside of the function.

Return type Function In Java
Return type Function In Java
Example:
import java.util.*;
Class A
{
     int t()
     {
         Scanner sc = new Scanner (System.in);
          System.out.print ("enter value");
          int a = sc.nextInt();
          System.out.print ("enter value");
           int b = sc.nextInt();
            int c = a+b;
            return c;
             }
         public static void main (String s[])
          {
             A d = new A();
              int k = d.t();
              System.out.print ("sum is " + k);
                }
         }

Non-Return Type Function

Non Return Type Functions in java
Non Return Type Functions in java

Non-Return type is a function that declares with void keyword and it doesn’t return any value after processing the instruction.

Example:-
import java.util.*;
Class KO
{
    void t()
     {
         Scanner sc = new Scanner (System.in);
          System.out.print ("enter value");
          int a = sc.nextInt();
          System.out.print ("enter value");
          int b = sc.nextInt();
          int c = a+b;
          System.out.print ("sum is" + c);
           }
     public static void main (String s[])
     {
         KO d = new KO();
           d.t();
            }
}

 

KO d = new KO()
It is an instruction that allow to create an object of class for executing its function. Here, KO is a class and d is an object.
d.t();
It is an instruction that execute existing function. Here, d is an object and t is function.

Parameter Functions

Parameter Function In Java
Parameter Function In Java

Parameter function is a type of function that accepts the input from another function and processes the data. We define the parameter variable to hold the value which is passed at the time of function calling as an argument.

Example:-
import java.util.*;
Class B
{
    void t(int x, int y)
     {
         int z = x+y;
         System.out.print("The sum is " + z);
           }
      public static void main (String s[])
      {
         Scanner sc = new Scanner (System.in);
         System.out.print ("enter value");
          int a = sc.nextInt();
          System.out.print ("enter value");
           int b = sc.nextInt();
            B x = new B();
             x.t(a, b);
             }
}

Static Function:

Static Methods In Java
Static Methods In Java

Static Function is a function that declares with static keyword and executes on the base of the class level. It means we can execute the function without an object. We use class name and function name by using dot(.) separator.

Example:-
import java.util.*;
Class B
{
   static int c;
   static void s()
   {
      Scanner sc = new Scanner (System.in);
      System.out.print ("enter value");
      int a = sc.nextInt();
      System.out.print ("enter value");
      int b = sc.nextInt();
       c = a+b;
      System.out.print ("sum is" + c);
      }
}
Class A
{
   public static void main (String s[])
    B.s();
    {
        if (A.c>0)
        {
           System.out.print ("positive value");
             }
       else {
          System.out.print ("negative value");
            }
    }
}

Final Function

Final Functions Usage
Final Functions Usage

The function declares with the final keyword that cannot be overridden at another class.

Example:-
import java.util.*;
Class A
{
   public static void main (String s[])
    {
        final int a=10;
        Scanner sc = new Scanner (System.in);
        System.out.print ("enter value");
         a = sc.nextInt();
        System.out.print ("input is" + a);
         }
}

About the author

Udit Mutreja

Add Comment

Click here to post a comment