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:
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: