Thursday, November 21, 2013

User Defined Function




Java provides vast numbers of ready to use built-in functions that can be utilize to solve math, string and other problems. Most of java built-in functions are imported from the default package java.lang. But as a programmer we too can write user defined packages and user defined functions. UDF (user defined function) make the programs easier to understand, easy to debug and write.

Java program execution start from main function and any other User defined function would execute if it called. When a UDF is called, it transfer control to the UDF from caller method. While calling a function, we may or may not send some parameter and the called function may or may not return any data. While calling a method the parameter passed is known as actual parameter and the parameter mention in called function is known as formal parameter.

User defined function has three parts:

 Method signature, method name and the parameters.

Example :

//Without parameter and return statement

import java.io.*;
    public class UDF_1
        {
            public static void main(String[] args) throws IOException
                {
                    Sum();
                }


       public static void Sum() throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    int a,b;
                    System.out.println("Enter two No ");
                    a=Integer.parseInt(br.readLine());
                    b=Integer.parseInt(br.readLine());                   
                    System.out.println("\n\tSum of : "+a+" and "+b+" is = "+(a+b));
                }
      }               




//Calling function with Parameters

import java.io.*;
    public class UDF_2
        {
            public static void main(String[] args) throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    int a,b;
                    System.out.println("Enter two No ");
                    a=Integer.parseInt(br.readLine());
                    b=Integer.parseInt(br.readLine());                      
                    Sum(a,b);
                }


       public static void Sum(int x, int y) throws IOException
                {
                 
                    System.out.println("\n\tSum of : "+x+" and "+y+" is = "+(x+y));
                }
      }               



//With return statement

import java.io.*;
    public class UDF_3
        {
            public static void main(String[] args) throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    int a,b;
                    System.out.println("Enter two No ");
                    a=Integer.parseInt(br.readLine());
                    b=Integer.parseInt(br.readLine());    
                    //calling sum() and prinitng the value returned by the sum() function
                    System.out.println("\n\tSum of : "+a+" and "+b+" is = "+Sum(a,b));
                }
                //return type is integer
       public static int Sum(int x, int y) throws IOException
                {
                 
                    return(x+y);
                }
      }               





No comments: