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
- It is a keyword which is used to provide accessibility of function.
- There are Three types of access specifier.
- Public: No Security, public members access from outside of class.
- Private: It's Secure, access inside of class and relative class.
- 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(); }}
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, nextFloat, nextChar 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=40Multiplication=40Multiplication=40Multiplication=40
$$$$$ -- OUTPUT -- $$$$$
Multiplication=40
Multiplication=40
Multiplication=40
Multiplication=40
Passing array to Function
// 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