Page 251 - ComputerScience_Class_11
P. 251
9.5 DEFINING A FUNCTION
There are three different ways of defining a function which are discussed as follows:
a. The function does not take any value as parameters and does not return any value.
For example,
import java.util.*;
class factor
{
void factor_num()
{
Scanner sc=new Scanner(System.in);
int num,i;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
System.out.println(i);
}
}
}
void main(String[] args)
{
factor_num();
}
}
b. The function takes the values as parameters but does not return any value.
For example,
import java.util.*;
class circle
{
void area (double r)
{
double a;
a=3.142*r*r;
System.out.println("Area: " +a);
}
void circumference (double r)
{
double c;
c=2*3.142*r;
System.out.println("Circumference: "+c);
}
void main(String[] args)
{
area(6); // function calling with parameters
circumference(18.6);
}
}
In the above program, the methods area() and circumference() are called along with parameter which calculates the
respective parametric values.
Methods and Constructors 249

