Java Reference
In-Depth Information
public static void
main( String [ ]
arguments )
{
...
}
}
Let us see some concrete examples.
3.2.2 Examples of basic functions
Let us create a demonstration program class
FunctionDeclaration
by typing
in the
FunctionDeclaration.java
text file the following code:
Program 3.2
A basic demonstration class for defining and calling static
functions
class
FunctionDeclaration
{
static int
square(
int
x)
{
return
x
∗
x;
}
static boolean
isOdd(
int
p)
{
if
( ( p%2)==0)
return false
;
else return true
;
}
static double
distance(
double
x,
double
y)
{
if
(x
>
y)
return
x
−
y;
else return
y
−
x;
}
static void
display(
double
x,
double
y)
{
System . out . println (
"("
+x+
","
+y+
")"
);
return
;
// return void
}
public static void
main ( String [ ]
args )
{
display (3 ,2) ;
display(square(2) ,distance (5 ,9)) ;
int
p=123124345;
if
(isOdd(p) )
System . out . println (
"p is odd"
);
else
System . out . println (
"p is even"
);
}
}
Compiling and executing this code, we get:
(3.0,2.0)
(4.0,4.0)
pisodd
Search WWH ::
Custom Search