| Lesson 5 -- Procedure & Function Summary |
|---|
|
The key benefits to PROCEDURES and FUNCTIONS come from reusing code within
you PROGRAM. If we needed to add two integers in dozens of places in our
code we could call the add2ints FUNCTION in each place and the FUNCTION
would do this job for use. If the task that add2ints did was more complex it
would make the program much easier to work with. In general when you find
yourself doing the same thing in more than one place in your code ask
yourself if a PROCEDURE or FUNCTION could consolidate this code to one
location and simplify the code. The other advantage to using PROCEDURES and FUNCTIONS is the same that is gained in using separate MODULES for your code. Once a PROCEDURE or FUNCTION works you can concentrate on other areas of the code. As long as you don't change the PROCEDURE or FUNCTION you can call it over and over again and be confident it works. The bottom line is that it is easier to work with a small 10-20 line PROCEDURE then to try to find a problem in a 150 line piece of code. Smaller is better. Don't break your code down into PROCEDURES or FUNCTIONS just because you can. Do it when it makes the code easier to work with. As a beginner, you may not know when to do this but in time you will become comfortable with these concepts. Hopefully you have learned some of the basics of PROCEDURES and
FUNCTIONS. You should also see why I included them in this series of tips on
flow control. While their primary purpose may not be as a flow control tool
they certainly do work to change the flow of a program. Remember that the
DCAL manual is filled with hundreds of PROCEDURES and FUNCTIONS that you can
use the same way we used the simple ones in our example. Browse through the
manual and you will see how many of the PROCEDURES or FUNCTIONS could be
used in your macros. You don't need to do anything special to use a built-in
PROCEDURE or FUNCTION, just call it from your code and it will operate as
described in the manual. If it doesn't, tell Dave. End of Lesson 5 -- Procedures and Functions |
