5.4 Execution of Functions


5.4 Execution of Functions

In every function call (or method invocation), the function that calls another function is known as the calling function; the second function is known as the called function. When a function calls or invokes another function, the normal execution flow of control in the first function is interrupted. The flow of control is altered and the second function starts execution. When the called function completes execution, the flow of control is transferred back (returned) to the calling function. This function continues execution from the point after it called the second function.

5.4.1 Object Interactions

The calling function and the called function can belong to two different objects, or to the same object. Objects interact with one another by sending messages. The object that sends the message is the requestor of a service that can be provided by the receiver object. A message represents a request for service, which is provided by the object receiving the message. The sender object is known as the client of a service and the receiver object as the supplier of the service.

The purpose of sending a message is to request some operation of the receiver object to be carried out; in other words, the request is a call to one of the operations of the supplier object. This is also known as method invocation. Objects execute operations in response to messages and the operations carried out are object specific.

For example, an object of class Person sends a message to an object of class Ball. In this message, some function (operation) of the first object calls function move of the second object. A message is always sent to a specific object, and it contains three parts:

  • The function (operation) to be invoked or started, which is the service requested; this must be a public function if invoked by another object

  • The input data required in order for the operation to start; this data is known as the arguments

  • The output data, which is the reply to the message; this is the actual result of the request

To describe the general interaction between two or more objects (the sending of messages between objects), one of the UML diagrams used is the collaboration diagram; see Figure 5.3 as an example. Another UML diagram used to describe object interaction is the sequence diagram.

click to expand
Figure 5.3: Collaboration diagram with three objects.

5.4.2 Categories of Functions

The most obvious categories of functions discussed so far are the private and public functions. Only the public functions of an object can be invoked from another object. The private functions are sometimes called internal functions because they can only be invoked by another function of the same object.

The instructions in a function start execution when the function is called or invoked. After completion, the called function may or may not return a value to the calling function. From the data transfer point of view, there are three general categories of functions:

  1. Simple (or void) functions do not return any value when they are invoked. The previous example, function display_message, is a simple or void function because it does not return any value to the function that invoked it.

  2. Value-returning functions return a single value after completion.

  3. Functions with parameters require one or more data items as input values when invoked.

The most general category of functions is one that combines the last two categories just listed—functions that return a value and that have parameters.

Another important criterion for describing categories of functions depends on the purpose of the function. There are two such categories of functions:

  • Accessor functions return the value of an attribute of the object without changing the value of any attribute(s) of the object. For example, the following are accessor functions: get_color, get_size, and show_status of class Ball in Section 4.11.1.

  • Mutator functions change the state of the object in some way by altering the value of one or more attributes in the object. Normally, these functions do not return any value. For example, the following functions are mutator functions: move and stop of class Ball in Section 4.11.1.

It is good programming practice to define the functions in a class as being either accessor or mutator.

5.4.3 Invoking a Simple Function

A simple (also called void) function does not return a value to the calling function. The simplest function of this kind is function display_message, discussed previously. There is no data transfer to or from the function. The KJP statement to call or invoke a void function that is referenced by an object reference is:

       call   function_name  of  object_ref  

For example, suppose the function display_message that belongs to an object referenced by myobj is invoked from function main, the call statement is:

       call display_message of myobj 

Figure 5.4 shows the calling mechanism. In the figure, the calling function is main, and the called function is display_message. After completing its task, the called function returns the flow of control to the calling function.

click to expand
Figure 5.4: Calling a 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