Introduction to Procedures
|
|
A procedure is an assignment that you want the compiler to perform
before providing a complete and reliable result that other parts of the
program can use. To create a procedure, the most fundamental syntax you
can use is as follows:
Procedure ProcedureName;
begin
end;
The procedure keyword is required to let the compiler know that you are
creating a procedure. You must also provide a name to the procedure. The
name follows the same rules we have applied to our variables so far.
Here are some suggestions:
- Use a name that specifies what the procedure is expected to
do. Usually, a verb is appropriate if it performs an action; examples
would be add, start, assign, play, etc.
- If the assignment of the procedure is a combination of
words, such as converting a temperature from Celsius to Fahrenheit,
start the name of the function with a verb and append the necessary
words each starting in uppercase (remember that the name of a procedure
must be in one word). Examples include
converttofahrenheit, calculatearea, loadfromfile.
- To make the name more readable, if it involves many words, you can include an underscore between them. Examples would be
convert_to_fahrenheit, calculate_area, load_from_file.
- Like a variable, you can start the name with an underscore. Examples are _height or _pursue.
Besides these suggestions, you can adopt a more personal naming
convention that suits you as long as you respect Object Pascal rules on
objects names. In this book:
- The name of a procedure will start in uppercase; examples are Load, Commit, Severe
- if the name is made of various words, each word will start in uppercase; examples include
DefaultName, BeforeConstruction, or MethodOfAssignment.
The assignment that the procedure must perform is included between the
begin and end keywords. The section between begin and end is referred to
as the body of the procedure.
Between the begin and the end keywords, you can define the assignment
the procedure will perform. For example, you can use the write procedure
to display a string or to request something from the user. Here is an
example:
|
program Project1;
{$APPTYPE CONSOLE}
Procedure StopHere;
begin
write('Press any key to continue...');
readln;
end;
begin
end.
Here is another example
program GCS3;
uses
SysUtils;
{$APPTYPE CONSOLE}
procedure Welcome;
begin
Writeln('Welcome to the world of Object Pascal Programming');
end;
begin
Write('Press any key to continue...');
Readln;
end.
|
|
After creating a procedure, you can use its assignment by "calling" it.
To call a procedure such as the one above, you must first decide in
which part of the program you want to call the procedure. For example,
to call the above StopHere procedure from the main body of the program,
you can simply type the name of the procedure as follows:
|
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
Procedure StopHere;
begin
write('Press any key to continue...');
readln;
end;
begin
StopHere
end.
No comments:
Post a Comment