Skip to main content

Chapter -14 What is Function and explain all types.

Function in Java with example. 

A Function is a block of code which only runs when it called. 

Topic 

  • Function
  • Why use Function
  • Syntax of Function
  • Types of Function 
  • Function with no return type and no parameter
  • Function with no return type and with parameter
  • Function with return type and with no parameter
  • Function with return type and with parameter
  • Call by Value
  • Passing Array to Function
  • Recursion 

Function: 

  • It is collection of statement that performs an specific tasks. 
  • In Other word, A Function in Java is similar to a procedure. 
  • Procedure means :- A set of instruction perform a specific task.
  • It execute of all value when it is called by its name.
  • Function perform, A large program is divided 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 important features of function is code re-usability.  
  • The JAVA library provides many predefined functions.

Why use Function: 

  • To avoid rewriting the same logic and again.
  • To keep track of what we are doing in a program.
  • To test and check logic independently. 

Syntax of Function 


Access Specifier:

  • It is a keyword which is used to provide accessibility of function.
  • There are Three types of access specifier.
  1. Public: No Security, public members access from outside of class.
  2. Private: It's Secure, access inside of class and relative class.
  3. Protected: Less Secure, access inside of derived class member. 
  • Unit -3 in OPPs Topics discussed in details of access specifier. 

Return Types:

  • 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.

Parameter List: 

  • It is the place where we can pass a number of parameter/ variable. 
  • These variable 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 Arguments 

Body:

  • It is the place where the actual code is written to perform the specific task.

Demo 1 

 
    // function without parameter
    public void add(){
    int z,  x=10, y=20;
    z=x+y;
    System.out.println("Addition =" +z);

  • Here : public is access specifier void is return type add is function name.
  • An Example of function without parameter 

class FunD1{
    public void add(){
    int z,  x=10, y=20;
    z=x+y;
    System.out.println("Addition =" +z);
}
    public static void main(String arg[]){
        FunD1 obj=new FunD1();
        obj.add();
    }
}

$$$$$ -- OUTPUT -- $$$$$


Addition =30

Demo 2


// demo 2 Function witḥ parameter
    public void add(int x,int y){
    int z;
    z=x+y;
    System.out.println("Addition =" +z);


  • Here : public is access specifier void is return type add is function name. y and x are the parameter which value is passed at the time of calling.
  • An example of function with parameter.

class FunD2{
    public void add(int x,int y){
    int z;
    z=x+y;
    System.out.println("Addition =" +z);
}
    public static void main(String arg[]){
        FunD2 obj=new FunD2();
        obj.add(10 , 20);
    }
}


$$$$$ -- OUTPUT -- $$$$$


Addition =30

Types of Function

There are two types of function:

1.Predefined Function

2.User-defined Function 

1.Predefined Function: 

  • The Function which is predefined in the library is called predefined function.
  • Example of predefined function print, println, nextInt, nextFloatnextChar etc.  

2.User-defined Function: 

  • The Function which is made by the user call is called User-defined function.
  • Example of User-defined function add(), sub(), mul(), div() etc. 
  • There are four more category of user-defined function.

1.Function with no return type and no parameter: 

  • As you can see from the above syntax in place of return type void(no return type) keyword is used and there are no parameter( ) in the place of parameter list.
  • For better understanding see the below example. 

// Example 1: no return type no parameter
class FunNo{
    public void add(){
    int z,  x=5, y=45;
    z=x+y;
    System.out.println("Addition =" +z);
}
    public static void main(String arg[]){
        FunNo obj=new FunNo();
        obj.add();
    }
}


$$$$$ -- OUTPUT -- $$$$$


Addition =50


2.Function with no 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  parameter( int a, int b) in the place of parameter list.
  • For better understanding see the below example. 

// Example 1.1: no return type with parameter
class FunNo1{
    public void add(int a, int b){
    int c;
    c= a+b;
    System.out.println("Addition =" +c);
}
    public static void main(String arg[]){
        FunNo1 obj=new FunNo1();
        obj.add(20, 36);
    }
}


$$$$$ -- OUTPUT -- $$$$$


Addition =56



3. Function with return type and with no parameter: 


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

// Example 1.2: no return type with no parameter
class FunNo2{
    public int add(){
    int z,  x=6, y=4;
    z= x+y;
    return  z;
}
    public static void main(String arg[]){
        FunNo2 obj = new FunNo2(); // creating object of add class
        int rs =obj.add();  
    System.out.println("Addition =" +rs);
    }
}


$$$$$ -- OUTPUT -- $$$$$


Addition =10


4.Function with return type and with parameter: 

  • As you can see from the above syntax in place of return type int(return integer type ) keyword is used and with two parameter( a and b ) in the place of parameter list.
  • For better understanding see the below example. 

// Example 1.3: with return type with parameter
class FunNo3{
    public int add(int a, int b){
        int c= a+b;
        return c;
    }
    public static void main(String arg[]){
        FunNo3 obj = new FunNo3(); // creating object of add class
        int rs =obj.add(4, 24);  
    System.out.println("Addition =" +rs);
    }
}


$$$$$ -- OUTPUT -- $$$$$


Addition =28


Call by Value  

  • In this type of calling of function directly value is passed at the time of calling. 
  • In another Word : Function call is way to tell the compiler to execute the  function body at the time. This program execute start from the main function in the sequence the instruction are written. 
  • For Better Understanding see an example. 

// Find a multiplication of two number,
class FunCV{
    public void mul(){
        int c, a=10, b=4;
        c = a*b;
        System.out.println("Multiplication=" +c);
}
    public static void main(String arg[]){
        FunCV cv = new FunCV();
        cv.mul();

    }
}


$$$$$ -- OUTPUT -- $$$$$


Multiplication =40

  • Note : Guys we can call more then times of function.
  • let's see an example:

//call by value Multiple times  :

class FunCV1{
    public void mul(){
        int c, a=10, b=4;
        c = a*b;
        System.out.println("Multiplication=" +c);
}
    public static void main(String arg[]){
        FunCV cv = new FunCV();
        cv.mul();
        cv.mul();
        cv.mul();
        cv.mul();

    }
}

$$$$$ -- OUTPUT -- $$$$$



Multiplication=40
Multiplication=40
Multiplication=40
Multiplication=40


    Passing array to Function 

    • I have already discuss  array and function with details. 
    • So, here now just see an example how to run both program with on program.

    // Function with arrays
    class FunAr{
        public void arr(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 value[]={10,20,30,40,50,60};    // Create arrays
                FunAr obj=new FunAr();              // Create object of class.
                obj.arr(value);
            }
    }


    $$$$$ -- OUTPUT -- $$$$$


    Total Sum =40


    • Here : In the above example all the elements of array  value will  be passed to array ar.


    Recursive 

    • The Process of calling a function by itself is called Recursive and the function that calls itself is called.
      Other word : Recursion is a process/ techniques of function call, so it is call itself. 
    • The techniques provides a way to break completed problems down into simple problems which are easier to solve. 
    • Recursion may be a bit difficult to understand.  
    • Let's see an example for better understanding.

    // table of 10 using condition statement;
    class FunRC1{
        public void table(int num){
            if(num<=100){
                System.out.println(num+ " ");
                num = num +10;  //increment of 10;
                table(num);         // self calling
            }
        }
        public static void main(String args[]){
            FunRC1 obj= new FunRC1();
            obj.table(10);     //calling of Function
        }
    }




    $$$$$ -- OUTPUT -- $$$$$


    10 20 30 40 50 60 70 80 90 100



    Practice Set:-  Q1. Find a factorial program using recursive function. 



    Comments

    Popular posts from this blog

    Assignment of ITA/ Information Technology and Application BCA- Technology369kk

    Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

    C++ and Java Practical All Questions Answers - BCA -Technology369kk

    C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

    Assignment of PMO (Principal of Management and Organization) - Technology369kk

     ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...