4.9 Definition of Functions


4.9 Definition of Functions

A program is normally decomposed into classes, and the classes consist of data declarations and definitions of functions (or methods).

Functions are the internal decomposition units. A function represents a small task in the overall solution. Figure 4.4 illustrates the general structure of a function. The body of a function consists of two basic parts:

  • The data declarations

  • The instructions that manipulate the data


Figure 4.4: General structure of a function.

The data declared within a function is known only to that function—the scope of the data is local to the function. Every function has its own data declaration and instructions that manipulate the data. The data in a function only exists during execution of the function; their persistence is limited to the lifetime of the function.

The purpose of the function is described in the paragraph that starts with the keyword description and ends with a star-slash (*/). The paragraph contains mainly text that explains the purpose of the function. Other comments can be included at any point in the function to clarify and/or document the algorithm. Line comments start with a double forward slash (//) and end with the line.

Every function has a name, which must be meaningful and should be the name of the task carried out by the function. The name of the function is declared in the function statement. This name is used when the function is called or invoked by some other function. The following KJP code defines the basic structure of a function. As before, the keywords are in boldface.

            description                   .  .  .            function  function_name  is              constants                  .  .  .              variables                  .  .  .              objects                  .  .  .              begin                  .  .  .            endfun  function_name  

The header of a function starts with the keyword function followed by the name of the function; the body of the function starts with the keyword is. The function ends with the endfun keyword and the name of the function.

The data declarations are divided into constant, variable, and object declarations. This is similar to the data declarations in the class. Constant, variable, and object declarations are optional. The instructions in the body of the function appear between the keywords begin and endfun.

One of the classes in every program must include a function called main. This function starts and terminates the execution of the entire program; it is the control function of the program.

In very simple and small programs that have only one class, the algorithm for the problem is implemented in function main—all the instructions for the solution of the problem are located in this function.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net