How To Explain Functions in Java With Examples

How To Explain Functions in Java With Examples
How To Explain Functions in Java With Examples


How To Explain Functions in Java With Examples

Function: A function is a collection of statements that performs a specific task. It executes when it is called by its name.


A large program is divided into several small building blocks for simplicity and this building block is called function. We can call a function again and again.

The most important feature of function is code reusability. The Java library provides many pre-defined functions.


Syntax of Java function

Access_specifier return_Type 

Function_Name(Parameter List)


{


Statement;


}


Function Declaration

Access Specifier: Access specifiers keyword which is used to provide accessibility of function. There are three access specifiers are used in java public, private, and protected.

Return Type: A operation could come at a price. The return_type is the knowledge variety of the worth of the operating returns. Some functions perform the required operations while not returning a price.

Function Name: This is the actual name of the operation. The operating name and also the argument(parameter) list along represent the operate signature.

Arguments: A parameter is sort of a placeholder. once an operation is invoked, you pass a price to the parameter. 

This price is mentioned as an actual parameter or argument. The parameter list refers to the sort, order, and range of the parameters of an operation. 

Parameters square measure optional; that's, an operation could contain no parameters.

Function Body: The operating body contains a group of statements that outline what the 
operate will.


The key point about the function. 

Function Declaration: At this stage, the function is said.

void data();

A operate declaration during which void(no come type) indicates there's no price returning by this operate and data is the name of operating.

Syntax of Function in Java?

public void data()

{

int x=20

int y=30;

int z=x+y;

System.out.println("Addition of data:"+z);

Here the public is an access specifier, avoid is the return type and data is the function name.
 

How To Use Function in java?

public void data(int x, int y)




int z;

int z=x+y;

System.out.println("Addition of data:"+z); 

} 

 

Here the public is the access specifier, the void is the return type and data is a function name,x and y are the parameters which value is passed at the time of calling.


Predefined Function in Java 

A predefined function in the library in Java Language is called a predefined function.

1.print()

2.println()

3.nextInt()

4.nextFloat()


User-defined Function in Java 

A User-defined function that is made by the user is called a user-defined function.

1.Addition()

2.Subtraction()

3.Multiplication()

4.Division()


Some Others Programming Language Functions





There are four categories of user-defined function

1. operate with no come kind and no parameter

2. Function is used no return type and with 
parameter.

3. Function with return type and no parameter.

4. operate with come kind and with parameter. 


Function no return type and no parameter 

The operation during which there's no parameter and there's no worth coming by that operation is termed operate with no return kind and no parameter.


How To Use No Return Type No Parameter Function in Java? 

class icoderweb

{

public int data()

{

int x=100,y=200;

int z=x+y;

System.out.println("Addition of data:"+z); 

}

public static void main(String[] args)

{

icoderweb obj=new icoderweb();

obj.data();



}


*****OUTPUT*****

Addition of data:300


Function with no return type and with parameter 

The operation during which there's no parameter and there's some worth coming by that operation is termed operate with return kind and no parameter.

How To Use  No Return Type With Parameter Function in Java?

class icoderweb 



public int data(int x, int y)



int z=x+y;

System.out.println("Addition of data:"+z); 



public static void main(String[] args) 

{

icoderweb obj=new icoderweb(); 

obj.data(100,200);



}


*****OUTPUT*****

Addition of data:200


In the above example, there are two parameters of integer type names x and y there at the time of calling two integer values will be passed in which first will assign to x and the second will assign to y.
variable z in-store data information.

Function with return type and no parameter 

The function in which there is no parameter and there is some value returning by that function is called Function with return type and no parameter. 

How To Use  Return Type No Parameter Function in Java?

class icoderweb 



public int data()



int x=100,y=200;

int z=x+y;

return z;



public static void main(String[] args) 

{

icoderweb obj=new icoderweb(); 

int r=obj.data(); 

System.out.println("Addition of data:"+r); 



}

*****OUTPUT*****

Addition of data:300

Above example, there is no parameter but the function will return an integer value because there is an int keyword in the place of return type and returned value will assign to variable z. 

Function with return type and with parameter 

The perform during which there's no parameter and there's some worth come backing by that perform is named perform with return kind and no parameter.


How To Use  Return Type With Parameter Function in Java?

class icoderweb 



public int data(int x, int y)



int z=x+y;

return z;



public static void main(String[] args) 

{

icoderweb obj=new icoderweb(); 

int r=obj.data(100,200); 

System.out.println("Addition of data:"+r); 



}

*****OUTPUT*****

Addition of data:200

Above example, there are two parameters and the function will return an integer value because there is an int keyword in the place of return type and returned value will assign to variable a. 
 

There are two ways of calling a function

1. Call By Value.

2. Call By Reference.  


Call by Value: In this type of calling a function direct value is passed at the time of calling.
In the decision by worth, the changes created informal parameters do not replicate in actual parameters. 

How To Use  Call By Value Function in Java?

class icoderweb 



public int data(int x, int y)



int z=x+y;

System.out.println("Addition of data:"+z); 



public static void main(String[] args) 

{

icoderweb obj=new icoderweb(); 

obj.data(100,200);



}


*****OUTPUT*****

Addition of data:300


Passing Array to Function 

Array Function: In this type of function, there is an array in the place of the parameter.

example: void data(int a[5])] and its value are passed at the time of calling. 


How To Use Array Function in Java? 

class icoderweb


public void ardata(int a[])


int s=0; 

for(int i=0;i<a.length;i++)

s=s+a[i]; 

System.out.println("Total Numberof Sum:"+s); 


public static void main(String[] args) 


int x[]={80,77,14,78,01}; 

icoderweb obj=new icoderweb(); 

obj.ardata(x);


}

*****OUTPUT*****

Total Numberof Sum:250

Above example, we can see that there is an array an [5] in place of parameter and there is another array.

x[5]={80,77 14,78,01and it is passed at the time of calling therefore the value of array x will be copied into the array a.

What is Recursion in java?

Recursion: The process of calling a function by itself is called Recursion and the function that calls itself is called Recursive Function. 


How To Find Factorial of any number in Java? 

class icoderweb 


public void table(int no) 


if(no<=50) 


System.out.print(no+" "); 

no=no+5;

table(no);

}


public static void main(String[] args) 


Icoderweb obj=new icoderweb(); 

//calling of the function 

obj.table(5);


}

*****OUTPUT*****

5 10 15 20 25 30 35 40 45 50


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.