Page 236 - Computer science 868 Class 12
P. 236
Different behaviour of primitive and object arguments:
As said earlier primitive parameters are sent as pass by value whereas object arguments are sent as pass by reference.
Now, when the variables are sent as primitive arguments, any change in the caller program. However, if the variables
then it will have no effect on the calling program are passed by reference, then any change in the value of the variable
will have the effect in main() also.
8.6 DEFINING A METHOD
Now, when a function name, its type and arguments are decided, its time to define a function. A function declaration
tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body
of the function. While defining a function, we may take parameters or not. Different ways of defining a function are:
• Methods take values as parameters
• Methods without parameters
• Methods with return statement
• Methods without return statement
1. Methods take values as parameters:
void calculate(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("The factorial of "+ n + " is: " +f);
}
2. Methods without parameters:
void prime()
{
int i,c=0;
for(i=1;i<=5; i++)
{
if(5%i==0)
{
c++;
}
}
if(c==2)
System.out.println(5 + " is a prime number");
else
System.out.println(5 + " is not a prime number");
}
3. Methods with return statement:
class find_palindrome
{
int palindrome(int a)
{
int r,s=0;
while(a>0)
{
r=a%10;
234234 Touchpad Computer Science-XII

