Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


2.5 Style, Reuse and Readability

The example has grown into two procedures, one called main and one called fred. However, we could get by with only one procedure:

PROC main()
  WriteF('My first program\n')
  WriteF('...slightly improved\n')
ENDPROC

What we've done is replace the call to the procedure fred with the code it represents (this is called inlining the procedure). In fact, almost all programs can be easily re-written to eliminate all but the main procedure. However, splitting a program up using procedures normally results in more readable code. It is also helpful to name your procedures so that their function is apparent, so our procedure fred should probably have been named message or something similar. A well-written program in this style can read just like English (or any other spoken language).

Another reason for having procedures is to reuse code, rather than having to write it out every time you use it. Imagine you wanted to print the same, long message fairly often in your program--you'd either have to write it all out every time, or you could write it once in a procedure and call this procedure when you wanted the message printed. Using a procedure also has the benefit of having only one copy of the message to change, should it ever need changing.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.