Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Notes For Structured Programming, Procedure and Function: Components

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 5

Notes for Structured Programming, Procedure and Function

By Tony Chun Kuen WONG (summarized from Chapter 8)

A) Modular Design Philosophy

If you own a stereo system, you are probably familiar with the concept of
components. A typical stereo system, for instance, includes a receiver, a tape deck,
a CD changer, and speakers. Manufacturers know that if they provide each
component separately, then consumers can mix and match the components as
necessary. Component stereo systems are also a more attractive alternative than
integrated systems (one-piece systems) because they can upgrade by simply
purchasing a new component. For example, you can easily replace a 3 CD-changer in
a component system with a 10-CD changer. Additionally, as technology improves and
new components become available, they can add to the system.

A stereo system component is analogous to a unit in the world of Object Pascal

programming. Modular programming refers to computer programming that is


based on a modular, or component-based, design that relies on code units.
Structured programs are programs that contain control structures, such as
decision and repetition structures, and execute statements sequentially; they typically
contain subprograms and may be modular in design. Thus all programs written in
Object Pascal are structured programs, because the language itself is highly
structured. Furthermore, all Windows applications written with the Delphi IDE are
modular programs, because the code for each form appears in a separate unit.

Modular programming offers several advantages. First, it allows the programmer to


logically organize a program by dividing the problem into smaller subproblems.
Repeatedly using this "divide and conquer" technique to solve a problem is

called Stepwise refinement and is part of the top-down design programming


methodology. For instance, a programmer may write a program consisting of three
subprograms : one subprograms contains the input routine, another performs
calculations, and final subprograms display the output. Second, modular programming
enhances the readability of the code and simplifies the debugging process. Third, as
with the components in a stereo system, a module can be easily replaced (or
upgraded) to improve the program. Finally, modular programming allows for the
reuse of code. Code reuse is an important aspect of computer programming. Once a
programmer writes a subprogram that performs a certain task, there is no need to
rewrite this subprogram in every application that requires it; instead, programmers can
simply reuse the existing code. This philosophy of reuse reemphasizes the importance
of adequate documentation, which must specify the required subprogram inputs and
the type of output.

B) Subprograms

Procedures and functions represent the two types of subprograms (or routines).
Let's review one of these routines, the division of two numbers in our calculator
problem :-

1: procedure TForm1.Button4Click(Sender: TObject);


2: begin
3: if strtofloat(edit2.text)= 0
4: then messagedlg('Can not divided by 0 !!!',mtinformation,[mbOK],0)
5: else edit3.text:= floattostr(strtofloat(edit1.text)/strtofloat(edit2.text));
6: end;

Remember, Delphi is designed as a development tool for the Windows operating


system. Events (key presses, mouse clicks, and so on) occurs in Windows, and
programmers must write event handlers to manage these events. An event handler
is a special type of procedure that is invoked (called or executed) when a specified
event occurs on its associated component. An event handler may be associated with
several events and components. In the preceding code,, line 1 is the declaration (or
heading) of the Button4Click procedure, an event handler for the button4 component.
We can see that it is an event handler rather than a standard procedure because
(Sender : TObject) appears in the declaration. This expression indicates that the
procedure is invoked by an event occurring on some Sender object of type TObject
(the base object class in Delphi). All event handlers in Delphi are procedures, so you
have been writing subprograms all along. This example also contains function calls.
They are strtofloat, floattostr and messagedlg
C) Arguments and Parameters

Let look at an user-defined procedures below. This time, the procedure Adder sum
two numbers.

{ Add num1 and num2 and store the result in sum}


Procedure Adder(num1 : real; num2 : real; var sum : real);
begin
sum := num1 + num2;
end;

In this code, num1, num2, and sum are the parameters of the Adder procedure.

Parameter act as placeholder for the information passed to a subprogram when it is


invoked. The parameter list specified in the Adder procedure contain three elements,
thereby informing the Delphi compiler that the Adder procedure requires three real
numbers as input.

A simple test driver for the Adder procedure appears below :-

Procedure TForm1.AddNumbersClick(Sender : TObject);


var
Firstnum, SecondNum, Result : Real;
begin
FirstNum := FloatToStr(edtNum1.text);
SecondNum := FloatToStr(edtNum2.text);
Adder(FirstNum, SecondNum, Result);
edtResult.Text := Format('%15.2',[result]);
end;

The variable FirstNum, SecondNum, and Result are the arguments of the Adder

procedures; these variables contains the values that are passed to the procedure. An
argument can consists of any expression that is of the same type as its corresponding
parameter.

Several important points apply concerning subprogram arguments and parameters :


a) The number of arguments must equal the number of parameters.
b) Order is important. The first argument corresponds to the first parameter, the
second argument to the second parameter, and so on.
c) The data type of each argument ,ust match the data type of its corresponding
parameter.
d) Names are not important. The name of the argument does not have to
correspond to the name of its parameter.
e) Recognize the manner in which data are passed – by reference or by

value.

Pass by reference is a type of parameter passing in which the memory location


(address) of the argument is passed to the subprogram instead of the argument’s
value, allowing the subprogram to access the actual variable and modify its content.

Pass by value is a type of parameter passing in which the value of the argument
is passed to the subprogram, allowing the subprogram to access a copy of the variable.
Pass by value preserves the contents of the original variable. It is the default method
of parameter passing in Object Pascal.

Arguments Parameters Passed by


FirstNum num1 value
SecondNum num2 value
Result Sum reference

Pass by

D) Defining and Using Subprograms

Procedures and function that are not built into Object Pascal are called user-defined
subprograms. The general form to defining a procedures is

Procedure ProcedureName(param1 : type1; param2 : type 2; ….);


[Local Declaration;]
begin
[statements;]
end;

To invoke a procedure, you state the procedure name along with any required
arguments. The general syntax of a procedure call follows :

ProcedureName(argument1, argument2, ….);


Functions differ somewhat from procedures. A function may take any number
of arguments, but it always returns a single value to the calling routine. A procedure,
on the other hand, does not automatically return a value. The general rule is to use a
function if you need to return exactly one value to the calling routine. For this reason,
our Adder procedure is better written as a function :

{ Add num1 and num2 and store the result in sum}


Function Adder(num1 : real; num2 : real) : Real;
begin
Adder := num1 + num2;
end;

A simple test driver for the Adder procedure appears below :-

Procedure TForm1.AddNumbersClick(Sender : TObject);


var
Firstnum, SecondNum, Result : Real;
begin
FirstNum := FloatToStr(edtNum1.text);
SecondNum := FloatToStr(edtNum2.text);
Result := Adder(FirstNum, SecondNum);
edtResult.Text := Format('%15.2',[result]);
end;

From this example, we see the general form of a user-defined function :

Function FunctionName (param1 : type1; ….) : FunctionType;


[Local Declarations;]
begin
[statements;]
FunctionName := ReturnValue;
end;

The general form of a function invocation with the returned value assigned to a
variable follows :

VariableName := FunctionName(argument1, argument2, …);

You might also like