![]() |
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.
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;
}
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.
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.
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.
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);
}
public void data()
{
int x=20
int y=30;
int z=x+y;
System.out.println("Addition of data:"+z);
}
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);
}
{
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
1.print()
User-defined Function in Java
A User-defined function that is made by the user is called a user-defined function.1.Addition()
Some Others Programming Language Functions
There are four categories of user-defined function
Function no return type 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
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();
}
}
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
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);
}
}
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
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
There are two ways of calling a function
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
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);
}
}