** Type of Methods in Java **
A method is a collection of statements that perform some specific task and
return the result to the caller. A method can perform some specific task without
returning any value.
According to the argument and return type of method there are four categories:
No Argument and No Return Type Method
Argument with No Return Type Method
No Argument and with Return Type Method
Argument with Return Type Method
No Argument and No Return Type Method:
In this category of method there are no arguments and the method does
not return any value so return type of this category of method is void.
Example:
Output of the above code as follows:
Sum =30
Argument with No Return Type Method:
In this category of method there are one or more arguments and the
method does not return any value so return type of this category of
method is void. In this category of method there are two terminologies:
Actual Parameter
Formal Parameter
Q. What is Actual Parameter of a Method?
Ans: The argument that we pass to the method body during function calling is
termed as Actual Parameter of the method. Here no data type is required only
the argument name is required.
Example: sum (x, y); // Here x and y are the Actual Parameter
Q. What is Formal Parameter of a Method?
Ans: The argument that we use to the method prototype during function
definition is termed as Formal Parameter of the method. Here data type is
required along with the argument name.
Example: sum (int x, int y); // Here x and y are the Formal Parameter
Example:
Output of the above code as follows:
Sum =30
No Argument and with Return Type Method:
In this category of method there are no arguments and the method
returns a value to the calling module. So return type of this category of
method is according to the return value from the method. To return any
value from the function body we use the return keyword.
The role of return statement as follows:
1. The return keyword is used to return a value to the calling module.
2. It also transfer the control from the function body to the calling module.
Syntax: return (value); Example: return 5;
OR return <variable name>; Example: return sum;
OR return (Expression); Example: return (20+5*4);
Example:
Output of the above code as follows:
Sum =30
Argument with Return Type Method:
In this category of method there are one or more arguments and the
method returns a value to the calling module. So return type of this
category of method is according to the return value from the method.
Example:
Output of the above code as follows:
Sum =30
In the above example the method int sum ( int a, int b) returns the sum of two
arguments a and b. Here the return statement return c; is termed as
Unconditional Return Statement in Java, because this type of return statement
returns a value without any condition.
Q. State the difference between return ; and return v ;
Ans:
return ; return v ;
1. Here return type of the method is void. 1. Here return type of the method
is according to the return value
from the method so it depends
upon value type of v.
2. It transfer the control from the method 2. It returns the value of v to the
body to the calling module & does not calling module and also transfer
return any value. the control there.
Q. What is Conditional Return Statement? Give an example.
Ans: If the method returns any value based on a given condition then there are
at least two return statements one for true condition and another one for false
condition. This type of return statement is termed as Conditional Return
Statement in Java program.
Output of the above code as follows:
Maximum value is = 20
So in the above code return a; and return b; is termed as Conditional Return
Statement in Java, because out of two return statements only one will be
executed based on the condition.