Functions in java or any other language helps to reduce the
lines of codes in our program.
Suppose we have to perform a particular task repeatedly but
not continuous repetition like loops then instead of writing those line of code
we used to make a function with some name just need to call it at the time of
our need.
Function has following parts:-
·
Name : As the name suggests, the function
should have proper name.
·
Definition : definition means function
should have those statements that are performing the required task.
·
Return type: return type says that what
type of value a function returns whether it is integer, character etc.
NOTE: Return type of a function could be void which means
it does not return nay value.
·
Arguments :the parameters on which the
task is to be performed.
NOTE: it is not necessary for a
function to have a arguments.
Syntax :
return_type function_name(arguments)
{
// statements
}
Example:
1.
class Function {
2.
void display (int n) //CREATION OF FUNCTION
3.
{
4.
System.out.println("Function with arguments
and without any return statement");
5.
}
6.
int show()
7.
{
8.
System.out.println("Function without
arguments but with return value");
9.
return 10;
10.
}
11.
public static void main(String [] args)
12.
{
13.
Function fn=new Function();
14.
int rtrn;
15.
fn.display(10); //CALLING FUNCTION display()
16.
rtrn=fn.show();// CALLING FUNCITON show()
17.
}
18.
}
Important points to remember regarding function:
PLEASE SHARE IT ON FACEBOOK,TWITTER AND OTHER SOCIAL NETWORKS AND RECOMMEND YOUR FRIENDS TOO.
Kishan Agarwal
Please Share
Tweet

No comments:
Post a Comment