21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

following list of requirements:<br />

■ an adequate set of primitives <strong>for</strong> defining procedural abstractions<br />

14.8 Methods 187<br />

■ safe and efficient mechanisms <strong>for</strong> controlling communication between program units<br />

■ simple, clearly defined mechanisms <strong>for</strong> controlling access to data objects defined<br />

within program units.<br />

Procedures and functions<br />

The basic procedural abstraction primitives provided in programming languages are<br />

procedures and functions. Procedures can be thought of as extending the statements of<br />

the language, while functions can be thought of as extending the operators of the language.<br />

A procedure call looks like a distinct statement, whereas a function call appears<br />

as or within an expression.<br />

The power of procedural abstraction is that it allows the programmer to consider the<br />

method as an independent entity per<strong>for</strong>ming a well-described task largely independent<br />

of the rest of the program. When a procedure is called, it achieves its effect by modifying<br />

the data in the program which called it. Ideally, this effect is communicated to the<br />

calling program unit in a controlled fashion by the modification of the parameters<br />

passed to the procedure. Functions, like their mathematical counterparts, return only<br />

a single value and must there<strong>for</strong>e be embedded within expressions. A typical syntax <strong>for</strong><br />

writing procedures and functions is shown below:<br />

void procedureName(parameters) {<br />

declarations<br />

procedure body<br />

}<br />

resultType functionName(parameters) {<br />

declarations<br />

function body<br />

return value;<br />

}<br />

It is critical that the interface between program units be small and well defined if we<br />

are to achieve independence between units. Ideally both procedures and functions<br />

should only accept but not return in<strong>for</strong>mation through their parameters. A single result<br />

should be returned as the result of calling a function.<br />

For example, to place text in a text box, use a procedure call as illustrated by the following<br />

code:<br />

setText("your message here");<br />

and a function call to obtain a value:<br />

String text = getText();<br />

>

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

Saved successfully!

Ooh no, something went wrong!