15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

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

584<br />

Creating Subprograms and Functions<br />

This loop runs at least once be<strong>for</strong>e checking a condition. If the condition is<br />

True, the loop stops.<br />

The REPEAT-UNTIL loop doesn’t need enclosing BEGIN-END keywords<br />

because the REPEAT and UNTIL keywords serve that function.<br />

Creating Subprograms and Functions<br />

You can create a subprogram (or a procedure) by using the PROCEDURE keyword<br />

as follows:<br />

PROCEDURE Name (Parameter list)<br />

Const<br />

(* Constants here *)<br />

Type<br />

(* Type definitions here *)<br />

Var<br />

(* Variable declarations here *)<br />

Begin<br />

(* Commands here *);<br />

End;<br />

Every subprogram must have a unique name, which usually describes the<br />

purpose of that subprogram (such as Calculate_Credit_Rating or<br />

DenyHealthBenefits). The parameter list declares variables to hold any<br />

data the procedure may need from another part of the program. For example,<br />

a simple parameter list might look like this:<br />

PROCEDURE Name (FirstName : string, Age : integer)<br />

Begin<br />

(* Commands here *);<br />

End;<br />

In the preceding example, a copy of the parameter is passed to the procedure.<br />

If the procedure changes the value of that data, the new value of that<br />

data appears only in the procedure and not in any other part of the program.<br />

If you want a procedure to change the value of a parameter, declare that variable<br />

in the parameter list with the Var keyword, such as<br />

PROCEDURE Name (FirstName : string, Var Age : integer);<br />

Begin<br />

(* Commands here *);<br />

End;<br />

A function is a special version of a subprogram that always returns a single<br />

value. To create a function, use the FUNCTION keyword, such as

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

Saved successfully!

Ooh no, something went wrong!