Page 231 - Computer science 868 Class 12
P. 231
• Return Type: If the function is returning a value, then the data type of the value is written before the method name.
The different data types written here are byte, short, int, long, float, double, char or Boolean. We can also write String
if we want to return a word or a sentence. Other than these, all non-primitive data types such as array, class, etc., can
also be used.
In case, if the function is not returning any value, then the keyword “void” is used.
Example: Return Type
public int perfect(int i)
{
int i,s=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
s=s+i;
}
}
return s; Return Statement
}
8.3.5 Method Name
Every method must be provided with a name so that it can be called from the same class or different classes. It should
not be any reserved keyword.
Example: public int perfect(int num)
8.3.6 Parameter List
The parameter list contains a list of the variables sent to the method for execution. When a function is called, the
values are sent from the calling program to the method in the same order as the data types mentioned. Within the pair
of parentheses, the data types along with the variable names are mentioned.
Example: public int perfect(int num)
In some functions, there may not be any parameters which may be known as an Empty Parameter list. In such methods,
the required variable should be mentioned in the method definition.
Example: public void show()
8.3.7 Body of the Method
Each method is used to do certain jobs which are a set of statements defined under the method header within the pair
of curly braces. This set of statements enclosed in curly braces under the method header is known as the body of the
method.
Example: public void show()
{
int p=perfect(6);
Body of the
if(p==6)
method
System.out.println("Perfect Number");
else
System.out.println("Not Perfect Number");
}
229
Methods 229

