JSL Functions and Re-Usable Code

There are many reasons to make code re-usable.  Even if you don’t want to re-use the code! I want to explore these reasons but first I need to introduce the nature of JSL syntax and the role of functions.

Let’s be clear about something: the JMP scripting language is a function-driven language.  By this I mean that almost everything we write in JSL is a function.  To make my point let’s take a look at a simple example:

msg = "hello world";
show(msg);

How many functions do we see here?  One?  For sure we are using the Show function.  What about the “=” sign.  Well that’s performing an assignment, but it’s just a short-hand operator for the Assign function:

Assign(msg, "hello world")

And what about the semicolon?  Surely that’s just a line terminator?  Actually JMP doesn’t care about line structure (given the amount of poorly written code that I have seen I can give testament to that!).  The semicolon indicates that one JSL statement has finished and the next is starting; it acts to glue to two statements together:

Glue( msg="hello world", show(msg) )

The point I’m trying to make here is that JSL is built around functions.  Even “standard” programming syntax such as if-statements and for-loops are implemented as functions.

What are the characteristics of a function?

In mathematics we can write y = f(x).  If we plug-in a value for “x” we get back the value “y”.  Functions in JSL (and any programming language) work the same way.  The “x” is referred to as a parameter or an argument to the function f (when we define x we talk about a parameter, when we use x we talk about an argument).  In programming as opposed to mathematics, we can give “f” a name which is descriptive of the function.  Finally functions return a value.

The number of arguments used by a function varies depending on its purpose.  The arguments are contained within parenthesis and separated by commas.  But even if the function doesn’t require any arguments the parentheses are still required (to distinguish function names from variable names).

Here are some example JSL functions:

lst = Files In Directory( path );

dir = Get Current Directory();

version = JMP Version();

y = Log(x);

nr = N Rows( myTable );

p = Normal Distribution(q);

sortedLst = Sort(lst);

now = Today();

thisWeek = Week Of Year( now );

Until now I have been talking about syntax – the rules that must be obeyed in order for our code to have correct structure.  But there is something much more important about functions, which is hopefully conveyed by the examples that I have shown above.

Independent of your knowledge of the JMP scripting language you can probably make a good guess at the purpose of each of the functions above.  So for example “Files In Directory” identifies the files in a directory and “Sort” could be used to arrange this list of files in ascending alphabetic order.

The fact that we can use multi-word names for our functions is plain weird if you have come from a traditional programming language but JMP doesn’t care about spaces so you can write “FilesInDIrectory” if that makes you more comfortable.  But however we choose to write our multi-word names the key thing is that they are descriptive of the purpose of the function.  This is such an important idea that I consider it to be the golden-rule of functions.

A function encapsulates a piece of coding logic that performs a task described by the function name.  In so doing, the function allows us to re-use the same code again and again in different contexts.  If it’s good enough for JMP why is it so many JSL programmers (and JSL training courses) totally ignore user-defined functions?  Why shouldn’t we create our own functions and re-use them in just the same way that we continually use the built-in JMP functions?  We should!  In my next blog I’ll explore the reasons (excuses) for not using functions, then I’ll look at the syntax of user-defined functions and most importantly I will explain  the golden-rule of function names.

Share the joy:
facebooktwittergoogle_pluslinkedinmailfacebooktwittergoogle_pluslinkedinmail

2 thoughts on “JSL Functions and Re-Usable Code”

Leave a Reply