Re: What is the main difference between a script and a procedure? [message #56105] |
Wed, 03 October 2007 06:40  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
mystea writes:
> As far as I can tell, a script:
> 1. Can't accept any arguments and can't take any extended loops.
> 2. It can recognize any variable that exists in the current session
> because it behaves just like a a list of commands in sequence.
>
> On the other hand, a procedure:
> 1. Can accept arguments, but can't recognize any variables which exist
> in current IDL session.
What you are calling a "script", most people call a
"batch file". This is a way to execute a series of
commands "as if" you were typing them at the IDL
command line. Since this is just about the most limited
way of using IDL, batch files are typically used infrequently.
More often people will put the same commands into
a file and add an END statement at the end of the file.
This file is now a "main-level program". It must be
compiled before it can be executed. Normally the
compile and execute is done with the .RUN executive
command. The big advantage of main-level programs over
batch files, is that you can include extended loops, etc.
in a main-level program without all the shenanigans
required to get a loop to work on the IDL command line.
As you become more sophisticated in your programming, you
will eventually realize that having all your variables
in one big pot is probably not such a great idea. (Especially
if you tend to name all your variables "a" to avoid a lot
of typing.) At that point, you might be interested in writing
procedures and functions (just another term for "IDL commands")
that do particular things for you, while at the same time,
keeping their internal variables from contaminating your
main-level working space.
IDL uses a "pass by reference" method of getting variables
into and out of commands, so it is easy to write procedures
and functions that change main-level variables, if that is
your purpose. You do, in fact, have to pass the variables into
the procedure or function via arguments or keywords, however,
since all the "action" occurs on a level separate from the
main level. (There are ways to access main-level variables
from within procedures and functions that don't involve passing
the variables, but this is rarely done, and only by experienced
programmers who REALLY know what they are doing and why they are
doing it.)
> However, I often run into the situation that I need a code which can
> recognize variables in current session *as well as* taking arguments.
> Is it possible to write such a code?
This is called "having your cake and eating it, too". It is
as easy to do in IDL as it is in life. :-)
And, anyway, what could your possibly pass to a batch file
that the batch file didn't already know about? The only thing
you can pass are things that exist at the main IDL level, and
the batch file already has access to all of that.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|