Let me Tell You 'Bout Let
The Let function is one of the most brilliant and underrated functions in FileMaker, and I think it should be used whenever and wherever possible. This article will give an introduction to the function, some possible scenarios when it should be used, and give an example as to it’s use.
Since the dawn of time, man has sought ways in which to simplify his life. That’s just a fact. Whether it is accurate or not is a question better left to the scholars. However someone at FileMaker was obviously adhering to this philosophy when they created the Let function.
To put it simply, the Let function can be used to simplify a calculation? How you ask? Well it does this by splitting out parts of a calculation into simpler, more easily readable parts. Think of it like an instruction manual for building a desk: The first thing you need to know is what all the parts are you require, before jumping in and putting those parts together (or something like that… close enough!)
FileMaker defines the format of the let function as follows:
So let’s break it down:
1) The function always begins with the word “Let” followed by open bracket. Everything within the function is contained within the opening and closing brackets, ie:
Ã¢ÂÂ¢ Let ( )
2) The function has two parameters, the first being variables, the second being the calculation itself, ie
Ã¢ÂÂ¢ Let ( Parameters ; Calculation )
3) The calculation part is simply a calculation that will be returned as the result. In fact, you could use a Let statement with no parameters. Doing this, would be pointless, but it illustrates what the calculation parameter does, ie:
Ã¢ÂÂ¢ Let ( “” ; 3 + 4 ) = 7
Ã¢ÂÂ¢ÃÂ Let ( “” ; “Blah” ) = “Blah”
4) You may have noticed, had you tried the above two functions, that they don’t actually work. The reason being that using a Let statement is pointless without having utilized the first parameter. Indeed it is the first parameter that is where the power of the Let statement lies.
A Good Time to Introduce VariablesÃ¢ÂÂ¦
The first parameter is Variables. Any programmer should be familiar with what variables are, and indeed any FileMaker developer should know or learn what they are also. In most simplest terms, a variable is something that stores the result of a calculation or function. Ponder the following:
myVariable = 3 + 4
Here, I have created a variable called “myVariable” and set it to 3 + 4. If I were to later check the contents of the variable, I would find it contains the number “7” as this is the result of 3 + 4.
myVariable = "String"
If I checked this variable, I would find exactly what it was set to, that being the word “String”. Variables can be set to literal text, numbers, calculations, functions you name it. Basically anything that can be specified within a calculation.
It should be noted at this time that variables occur in three forms within FileMaker:
Ã¢ÂÂ¢ Local variables (ie $Variable) , used in scripts to store results, numbers, text and so on…
Ã¢ÂÂ¢ Global Variables (ie $$Variable), also used in scripts, these retain their value for the duration of a users session once set.
Ã¢ÂÂ¢ Let statement variables. These only exist within the scope of the calculation in which they are used.
Back to the FunctionÃ¢ÂÂ¦
Variables are defined within the Let function in one of two formats, depending upon whether you are defining one, or more variables.
First, we will start with defining more than one variable, the format is as follows:
var1 = 1 + 2 ;
var2 = 2 + 3 ;
var3 = 3 + 4
var1 + var2 + var3
You can see I have split things out onto separate lines to make it more visible the format. When defining multiple variables, they must always be enclosed in square brackets. Within those brackets, each variable defined is separated from the next by a semicolon. Note also that the last variable does not require a semicolon, as there are no more variables after it.
Here, I have created three variables, and set them to various basic calculations. The first parameter of the function is everything contained within the square brackets. The second parameter you can see is the result I wish to return. I wish to return the three variables contents, added together.
In this basic case, the contents of the three variables are 3, 5, and 7 respectively. With the result defined to be var1 + var2 + var3, the end result will be 3 + 5 + 7, giving an overall result to the function of 15.
var1 = 4 ;
var2 = var1 * var1 ;
var3 = var2 / var1 ] ;
This example, though completely pointless, illustrates the fact that once a variable has been defined, it can be used in subsequent variables that are defined. [B]You cannot use a variable in a definition unless previously defined[/B]. Here, I have referenced var1 in the definition of var2, and referenced both var1 and var2, in the definition of var3. The result is to be var3.
Here is an example of a let statement in which only one parameter is defined:
param = Get ( ScriptParameter ) ;
param = 1 ; "One" ;
param = 2 ; "Two" ;
param = 3 ; "Three" ; "I Don't Know"
This example illustrates perhaps one of the most important reasons as to why you would want to use a Let function. Here, the Let function is only defining one parameter, called param. It has been set to the function Get ( ScriptParameter ) so this let statement is most likely being used in a script, where this function is used. The value of param is then checked for one of three values by using a Case statement, and the corresponding result is returned.
First thing you should note is that with one parameter, square brackets are not required. There exists only one semicolon here, and that separates the single variable definition, from the second parameter (that being the case statement).
Second thing you should ask yourself, is why bother using a Let statement here? The same calculation could just as easily have been done in the following manner:
Get ( ScriptParameter ) = 1 ; "One" ;
Get ( ScriptParameter ) = 2 ; "Two" ;
Get ( ScriptParameter ) = 3 ; "Three" ; "I Don't Know"
Both will return the exact same result. The reason why the Let function is better here, is for readability, and speed purposes. First, you will most likely see that the Let function is far more pleasing on the eye than the second function, as you don’t see the large Get ( ScriptParameter ) function on every line - it appears only once.
This leads into the second and most important reason - speed. In the Let function, we only reference the Get ( ScriptParameter ) function once, by setting it into a variable. From then on, it is the variable we are referencing, not the function. In the second calculation, we reference the Get ( ScriptParameter ) function three times.
What if we were referencing a far more complex calculation, or custom function, which took half a second to give the result? Well, the first Let function would take half a second. The second one would take 1.5 seconds, because everytime we are calling it, it has to be evaluated.
The key concepts here are:
Ã¢ÂÂ¢ If part of a calculation is going to be repeated many times, then it should be defined as a variable within a Let function
Ã¢ÂÂ¢ If a calculation is starting to look too complex or hard to understand, try splitting it into simpler components as variables using a Let function