Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

14.9 Parameter-passing mechanisms 189 Java provides the following scheme. All primitive data terms are passed by value, which is most commendable, but all proper objects are passed by reference. No distinction is made between procedures and functions. Thus a method of either type (procedure or function) can modify any non-primitive parameter and it is left to the programmer to enforce a discipline over changing parameters. A small concession is that the pointer to an object cannot be changed, for example to point to another object. Fortran employs only a single parameter passing mode: call by reference. Thus, undesirably, all actual parameters in Fortran may potentially be changed by any subroutine or function. The programmer is responsible for ensuring the safe implementation of input and output parameters. Using call by reference, the location of the actual parameter is bound to the formal parameter. The formal and actual parameter names are thus aliases; modification of the formal parameter automatically modifies the actual parameter. This is what you might expect of a language where arrays are often used, and the performance hit of copying arrays is unacceptable. Fortran also, unfortunately, restricts the type of result that may be returned from functions to scalar types only (i.e. not arrays etc.). Visual Basic.Net provides the choice of value or reference parameters, described by the key words ByVal (the default) and ByRef in the method header. But when objects are passed, they are always passed by reference. In C#, by default, primitive data items are passed by value, objects are passed by reference. But you can pass a primitive data item by reference if the parameter is preceded by the key word ref in both the method header and the method call. You can also precede an object name by ref, in which case you are passing a pointer to a pointer. This means that the method can return an entirely different object. Call by value-result is often used as an alternative to call by reference for input-output parameters. It avoids the use of aliases at the expense of copying. Parameters passed by value-result are initially treated as in call by value; a copy of the value of the actual parameter is passed to the formal parameter, which again acts as a local variable. Manipulation of the formal parameter does not immediately affect the actual parameter. On exit from the procedure, the final value of the formal parameter is assigned into the actual parameter. Call by result may be used as an alternative to call by reference for output parameters. Parameters passed by value are treated exactly as those passed by value-result except that no initial value is assigned to the local formal parameter. Ada identifies three types of parameter: ■ input parameters to allow a method read-only access to an actual parameter. The actual parameter is purely an input parameter; the method should not be able to modify the value of the actual parameter ■ output parameters to allow a procedure write-only access to an actual parameter. The actual parameter is purely an output parameter; the procedure should not be able to read the value of the actual parameter ■ input-output parameters to allow a procedure read-and-write access to an actual parameter. The value of the actual parameter may be modified by the procedure. Ada only allows input variables to functions. The parameter-passing mechanisms used in Ada (described as in, out and in out) would therefore seem to be ideal. However,

190 Chapter 14 ■ The basics Ada does not specify whether they are to be implemented using sharing or copying. Though beneficial to the language implementer, since the space requirements of the parameter can be used to determine whether sharing or copying should be used, this decision can be troublesome to the programmer. In the presence of aliases, call by valueresult and call by reference may return different results. 14.10 ● Primitive data types Programmers are accustomed to being provided with a rudimentary set of primitive data types. These are provided built in and ready made by the programming language. They usually include: ■ Boolean ■ char ■ integer ■ real or floating point. These data types are accompanied by a supporting cast of operations (relational, arithmetic, etc.). For each type, it should be possible to clearly define the form of the literals or constants which make up the type. For example, the constants true and false make up the set of constants for the type Boolean. Similarly, we should be able to define the operations for each type. For the type Boolean, these might include the operations =, , not, and, and or. In most languages the primitive data types are not true objects (in the sense of objects created from classes). But in Eiffel and Smalltalk, every data type is a proper object and can be treated just like any other object. For certain application domains, advanced computation facilities, such as extended precision real numbers or long integers, are essential. The ability to specify the range of integers and reals and the precision to which reals are represented reduces the dependence on the physical characteristics, such as the word size, of a particular machine. This increases the portability of programs. However, some languages (for example C and C++) leave the issue of the precision and range of numbers to the compiler writer for the particular target machine. Java gets around this sloppiness by precisely defining the representation of all its built-in data types. Whatever machine a program is executed on, the expectation is that data is represented in exactly the same manner. Thus the program will produce exactly the same behavior, whatever the machine. 14.11 ● Data typing A data type is a set of data objects and a set of operations applicable to all objects of that type. Almost all languages can be thought of as supporting this concept to some extent. Many languages require the programmer to define explicitly the type (e.g. integer or character) of all objects to be used in a program, and, to some extent or another, depending on the individual language, this information prescribes the operations that

14.9 Parameter-passing mechanisms 189<br />

Java provides the following scheme. All primitive data terms are passed by value, which<br />

is most commendable, but all proper objects are passed by reference. No distinction is<br />

made between procedures and functions. Thus a method of either type (procedure or<br />

function) can modify any non-primitive parameter and it is left to the programmer to<br />

en<strong>for</strong>ce a discipline over changing parameters. A small concession is that the pointer to<br />

an object cannot be changed, <strong>for</strong> example to point to another object.<br />

Fortran employs only a single parameter passing mode: call by reference. Thus,<br />

undesirably, all actual parameters in Fortran may potentially be changed by any subroutine<br />

or function. The programmer is responsible <strong>for</strong> ensuring the safe implementation<br />

of input and output parameters. Using call by reference, the location of the<br />

actual parameter is bound to the <strong>for</strong>mal parameter. The <strong>for</strong>mal and actual parameter<br />

names are thus aliases; modification of the <strong>for</strong>mal parameter automatically modifies<br />

the actual parameter. This is what you might expect of a language where arrays are<br />

often used, and the per<strong>for</strong>mance hit of copying arrays is unacceptable. Fortran also,<br />

un<strong>for</strong>tunately, restricts the type of result that may be returned from functions to<br />

scalar types only (i.e. not arrays etc.).<br />

Visual Basic.Net provides the choice of value or reference parameters, described by<br />

the key words ByVal (the default) and ByRef in the method header. But when objects<br />

are passed, they are always passed by reference.<br />

In C#, by default, primitive data items are passed by value, objects are passed by reference.<br />

But you can pass a primitive data item by reference if the parameter is preceded<br />

by the key word ref in both the method header and the method call. You can also precede<br />

an object name by ref, in which case you are passing a pointer to a pointer. This<br />

means that the method can return an entirely different object.<br />

Call by value-result is often used as an alternative to call by reference <strong>for</strong> input-output<br />

parameters. It avoids the use of aliases at the expense of copying. Parameters passed by<br />

value-result are initially treated as in call by value; a copy of the value of the actual parameter<br />

is passed to the <strong>for</strong>mal parameter, which again acts as a local variable. Manipulation<br />

of the <strong>for</strong>mal parameter does not immediately affect the actual parameter. On exit from<br />

the procedure, the final value of the <strong>for</strong>mal parameter is assigned into the actual parameter.<br />

Call by result may be used as an alternative to call by reference <strong>for</strong> output parameters.<br />

Parameters passed by value are treated exactly as those passed by value-result except that<br />

no initial value is assigned to the local <strong>for</strong>mal parameter.<br />

Ada identifies three types of parameter:<br />

■ input parameters to allow a method read-only access to an actual parameter. The<br />

actual parameter is purely an input parameter; the method should not be able to<br />

modify the value of the actual parameter<br />

■ output parameters to allow a procedure write-only access to an actual parameter.<br />

The actual parameter is purely an output parameter; the procedure should not be<br />

able to read the value of the actual parameter<br />

■ input-output parameters to allow a procedure read-and-write access to an actual<br />

parameter. The value of the actual parameter may be modified by the procedure.<br />

Ada only allows input variables to functions. The parameter-passing mechanisms used<br />

in Ada (described as in, out and in out) would there<strong>for</strong>e seem to be ideal. However,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!