Page 233 - Computer science 868 Class 12
P. 233
int s;
int add(int a, int b)
{
s = a+b;
return s;
}
void main()
{
method obj = new method();//Creating an object of the class
int r=add(20,100); //invoking instance method
System.out.println("The sum is: "+r);
}
}
Output:
The sum is: 120
3. Pure method: A pure function is a function if it does not change the state of an object and also returns a value to the
function from where it is called which depends on the input parameters. Some in-built pure methods are charAt(),
length(), max(), min() etc. We can also create a user-defined pure method.
Let us take the following example.
int add(int a, int b)
{
s = a+b;
return s;
}
The above method returns s.
4. Impure method: An impure function is a function if it changes the state of an object, i.e., if the variable in the formal
parameter changes its value, then the variable of the actual parameter also changes. Also, it may or may not return
a value to the function from where it is called. An Impure function is also called a Mutator.
class factor
{
public void factor_sum(int a)
{
int i,s=0;
f=1;
for(i=1;i<=a;i++)
if(a%i==0)
{
s=s+i;
}
System.out.println(s);
}
}
The word parameter means arguments sent to a method on which the result of the function may depend. It acts as a
variable in the method. Parameters are of any data type both primitive and user-defined. Any number of parameters
may be sent to a method separated by a comma.
Variables when appearing in the caller method are known as Arguments; and when appearing in method definition
are said to be Parameters.
231
Methods 231

