Lesson 5 -- Procedure & Function Summary


The key benefits to PROCEDURES and FUNCTIONS come from reusing code within your 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 you. 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.

We will cover PROCEDURES and FUNCTIONS much more in future lessons.

To see how much of this material you truly understand, modify the add2ints FUNCTION (in the sample macro) so that it RETURNS a STRING containing the correct result. All the code you need is already in the sample. All that is required to complete this exercise is to change around the location of some code and change the FUNCTION declaration. This is not an 'easy' task but it should be possible if you understand the material we have covered in these tips to this point. If you have problems doing this post a message on the DDN Forum. If you can accomplish this 'assignment' then you are just about ready to start writing your own macros.

As with all these lessons, if you don't use it you won't get it. Do the exercises. Compile the samples. Break the code. Fix the code. Ask questions.

End of Lesson 5 -- Procedures and Functions

< Previous Lesson   |   Next Lesson >

DCAL Tutorial Contents



Thank you for printing this page. Please feel free to contact us for further assistance. You can call our sales department at +1 (800) 394-2231, Monday through Friday from 8:00 a.m. to 5:00 p.m. Eastern time or send an e-mail message to info@datacad.com.