On Tue, 29 Jun 2004, Cummings, Mike wrote:

> I'm in the middle of re-writing some code and realized that there has
> got to be an easier way.  After thinking about how to get through it all
> without a number nested loops and endless queries, I've realized that I
> may be missing some basics of programming ( I'm a geologist ) and could
> use some help.  

Well... I'm a geologist too, but if you have the skills to read the story 
of the world as seen in any canyon wall, you can think logically enough to 
learn programming.

> I'm thinking that I might be able to add a column(s) to the table and/or
> create a point using a custom function(s) and then use this data later
> in the process.  

You could. You'd use a function in an update SQL statement that takes
input from one or more fields in each record and returns a point object, 
effectively geocoding the record. Then you could use those points in 
subsequent queries to build new analyses.

> My questions are general ones: 
> 
> When do you call a subroutine? 

Use a subroutine (or a function) when you need to execute code that you
might want to call from more than one place. This reason follows the
principle: "Never write the same code twice." 

Another reason to call a subroutine is to simplify your high-level code 
so that it doesn't get cluttered with low-level details.

A geologic analogy you can see in the rocks is the sea-level transgression
sequence. You can describe in detail the upward-fining sequence from sand
to shale to limestone, but if you're trying to read the big picture, you
might mentally call it Sub SeaLevelRise() and not sweat the details. That
same pattern may occur several times in a section, so you can "call"  
SeaLevelRise() as many times as you like.

> When do use a custom function?  

You call a function when you want a value returned in-line in your code. 
Otherwise, it's used for the same reasons as a subroutine. For example,

if BLACK = IsTheColor() then
  print "None is the number."
end if

is more elegant (and poetic) than

Call IsTheColor( nColor )
if BLACK = nColor then
  print "None is the number."
end if

But the choice is often a matter of taste. And sometime it matters not 
very much. You can even return values through function parameters as well. 
I don't think there's any rigid rules here. When you want to use the call 
as an expression (or in an upate statement) choose the function.

> What are the "functional" differences?

The function can be evaluated inline as an expression. The subroutine 
doesn't return a value.

> What are the limitations? 

Functions can't return complex types like arrays or usefined types. These 
have to be passed as parameters.

There is a small overhaed cost in performance when you call functions or 
subroutines, but one is no better than the other. Not in MapBasic, at 
least. But the performance hit is pretty small compared to the cost you'll 
save making your code more flexible, easy to read and maintainable.

On the other hand, if you are writing disposable code (one-shot, use once
and throw away scrapplications) then stream-of-consciousness coding is
definitely faster to write. If this is the case, just churn out the code,
get the job done, and don't be another casualty of the seduction of the
art!

- Bill


---------------------------------------------------------------------
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Message number: 12405

Reply via email to