by Dinesh Thakur

In any programming, methods or functions are made to support modular programming. By making small functions or methods for each different job not only divides a large project into small manageable tasks but also helps in easy debugging. Methods also make a project scalable that is, it is very easy to expand an existing project which supports modular programming. The methods or a function that we make in java has to be called via objects.

Functions are of four types:

 

1. Without parameter, without return

2. Without parameter, with return

3. With parameter, without return

4. With parameter, with return

 

Function Without Parameter, Without Return

class RectUsingFun
{
            void getarea()
            {
                        int l,b,a;
                        l=50;
                        b=8;
                        a=l*b;
                        System.out.println("Area of rectangle is : " +a);
            }
            public static void main(String args[])
            {
                        RectUsingFun RectFun = new RectUsingFun ();
                        RectFun.getarea();
            }
}
Output of Above Program is:
Area of rectangle is: 400
Function Without Parameter, With Return
class FunOutPar
{
            int getarea()
            {
                        int l,b,a;
                        l=50;
                        b=8;
                        a=1*b;
                        return(a);
            }
            public static void main(String args[])
            {
                        int ARect;
                        FunOutPar FunPar = new FunOutPar();
                        ARect=FunPar.getarea();
                        System.out.println("Area of Rectangle is : "+ARect);
            }
}
Output of Above Program is:
Area of Rectangle is: 8
Function With Parameter, Without Return
class FunWithPar
{
            void getarea(int l,int b)
            {
                        int area;
                        area=1*b;
                        System.out.println("Area of Rectangle is : "+area);
            }
            public static void main(String args[])
            {
                        FunWithPar FunPar = new FunWithPar();
                        FunPar.getarea(50,10);
            }
}
Output of Above Program is:
Area of Rectangle is : 10
Function With Parameter, With Return
class FunWithParWithRet
{
            int getarea(int l, int b)
            {
                        int a;
                        a=l*b;
                        return(a);
            }
            public static void main(String args[])
            {
                        int area;
                        FunWithParWithRet FunRet = new FunWithParWithRet();
                        area=FunRet.getarea(50,10);
                        System.out.println("Area of Rectangle is : "+area);
            }
}
Output of Above Program is:
Area of Rectangle is : 500
Function With Parameter, With Return
class AvgFunction
{
            float a;
            float getavg(int x,int y,int z)
            {
                        a=(float) (x+y+z)/3;
                        return(a);
            }
            public static void main(String args[])
            {
                        float Avg;
                        AvgFunction fun = new AvgFunction();
                        Avg = fun.getavg(10,20,30);
                        System.out.println("Average of Three Variables is : "+Avg);
            }
}
Output of Above Program is:
Average of Three Variables is : 20.0
           
           
Function With Parameter, With Return
import java.io.*;
class DiscQuadEq
{
            int d;
            void getdisc(int x,int y,int z)
            {
                        d=y*y-4*x*z;
                        System.out.println("Discriminate of Quadratic Equation is : "+d);
            }
            public static void main(String args[])
            throws IOException
            {
                        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
                        String h;
                        int a,b,c;
                        System.out.print("Enter Value of a : ");
                        h=bf.readLine();
                        a=Integer.parseInt(h);
                        System.out.print("Enter Value of b : ");
                        h=bf.readLine();
                        b=Integer.parseInt(h);
                        System.out.print("Enter Value of b :");
                        h=bf.readLine();
                        c=Integer.parseInt(h);
                        DiscQuadEq QuadEq = new DiscQuadEq();
                        QuadEq.getdisc(a,b,c);
            }
}
Output of Above Program is:
Enter Value of a : 5
Enter Value of b : 10
Enter Value of b : 3
Discriminate of Quadratic Equation is : 40