Java Reference
In-Depth Information
3.2 Declaring and calling functions
3.2.1 Prototyping functions
Functions should always be declared inside the body of a class: The program
class (for now). The generic syntax for declaring a
function
F
that takes
N
arguments
arg1
, ...,
argN
of respective types
Type1
, ...,
TypeN
and return a
result of type
TypeR
is:
static TypeR F(Type1 arg1, Type2 arg2, ..., TypeN argN)
{
TypeR result;
block_of_instructions;
return result;
}
Procedures are special functions that do not return any result. In Java, this
is specified by using the
void
keyword for the function return type. Thus the
declaration of a
procedure
is as follows:
static void Proc(Type1 arg1, Type2 arg2, ..., TypeM argM)
{
block_of_instructions;
return;
}
The last instruction statement
return;
in the procedure
Proc
may be omitted.
We always put the keyword
static
in front of procedure/function declarations:
static void Proc
. We will explain why this is necessary in Chapter 5 dealing
with objects and methods. Since procedures and functions shall be attached to
the body of class (that is, encapsulated into the class), we have basically the
following program skeleton:
Program 3.1
Basic program skeleton for defining static functions
class
ProgramSkeleton
{
static
TypeF F(Type1 arg1 ,
. . . , TypeN argN )
TypeF r e s u l t ;
// Description
Block
−
of
−
instructions ;
return
result ;
}
static void
Proc (Type1 arg1 , Type2 arg2 ,
. . . , TypeM argM)
{
block instructions ;
return
;
}
Search WWH ::
Custom Search