## Let me Tell You 'Bout Let

by

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:

Let({[}var1=expression1{;var2=expression2…]};calculation)

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:

Let (

[

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.

**Another example:**

Let ([

var1 = 4 ;

var2 = var1 * var1 ;

var3 = var2 / var1 ] ;

var3

)

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.

**And FinallyÃ¢Â€Â¦**

Here is an example of a let statement in which only one parameter is defined:

Let (

param = Get ( ScriptParameter ) ;

Case (

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:

Case (

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

## Comments

## Stuart Firth on 14 October, 2015

Excellent Daniel! I have been able to get my head around 'Let' recently, and you have now let me. (Pun intended) ! Cheers....

## CornĂ© van Bakel on 07 February, 2015

Thank you for the crystal-clear explanation of the Let statement! Finally it got to me!

## Daniel Wood on 10 February, 2015

No problem :)

## Scott on 30 August, 2015

Best description of Let I've ever read. I admit I use Case all the time. Maybe now, not so much. Well done!

## Pete on 04 February, 2015

Great explanation of Let. (Thanks!) I've been bypassing learning this function for awhile. But now I can understand that it will make my developing life easier and probably more important, a way to optimized the database.

## Something to say?

## Some HTML is OK

`<b>`

bold`</b>`

`<i>`

italic`</i>`

`<blockquote>`

`</blockquote>`

`<pre>`

`</pre>`