Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
Juggernaut

Global Variables use in Custom Functions - Just out of Curiousity

Featured Replies

Hi all -

I am just curious what your opinions are about the use of Global Variables in a custom function.

You can refer to global variables in a custom function, and you can also define them via a Let function.

I would be sure to include a check for IsEmpty( $$yourGlobalVar ) and also keep in mind the usual general cautions about usage of global variables.

Did you have a specific question?

  • 1 month later...

I'm a huge fan of both Global and local variables in custom functions. There's heavy use of both in my UUID custom functions for passing data between recursive calls and for saving data between calls: https://github.com/j...UIDTimeNIC.fmfn

The trick is always remembering to clean-up any variables I use when I'm done. I also usually wind up having to implement these types of custom functions as scripts first so I can work out the details with the debugger rather than trying to clutter a calculation with development exceptions for tracing what's going on.

  • 3 months later...

I'm a huge fan of both Global and local variables in custom functions. There's heavy use of both in my UUID custom functions for passing data between recursive calls and for saving data between calls: https://github.com/j...UIDTimeNIC.fmfn

The trick is always remembering to clean-up any variables I use when I'm done. I also usually wind up having to implement these types of custom functions as scripts first so I can work out the details with the debugger rather than trying to clutter a calculation with development exceptions for tracing what's going on.

Jeremy ; can you elaborate on how you do that? Or do just write the scripts and then convert them to the custom function.? this is definately something i am running into as it takes forever for me to test what is going on line by line.

thanks

Jeremy ; can you elaborate on how you do that? Or do just write the scripts and then convert them to the custom function.? this is definately something i am running into as it takes forever for me to test what is going on line by line.

thanks

Imoree, if I know that a custom function will be particularly tricky — something with several sequential steps or any looping process — I'll often prototype the functionality in a script first. That way, I can walk through step-by-step and see what values are in which variables every step of the way with the data viewer. Debugging a script is much easier than debugging an equivalent custom function, since you can't inspect intermediate states while a custom function is running. Once I have the algorithm working right in script form, I then reproduce it as faithfully as possible as a custom function.

Imoree, if I know that a custom function will be particularly tricky — something with several sequential steps or any looping process — I'll often prototype the functionality in a script first. That way, I can walk through step-by-step and see what values are in which variables every step of the way with the data viewer. Debugging a script is much easier than debugging an equivalent custom function, since you can't inspect intermediate states while a custom function is running. Once I have the algorithm working right in script form, I then reproduce it as faithfully as possible as a custom function.

appreciate the repsonse jeremy..

So in response again Jeremy; How would one go about putting this into a script to test for functionality and variable values?


Case (

	/*Pull node field from UUID, and set-up conversion loop*/

	not $~step;

		Let ( [

			~idAsNumber = GetAsNumber ( theID );

			~version = Left ( ~idAsNumber ; Length ( ~idAsNumber ) - 40 );

			$~nicAddress = Right ( ~idAsNumber ; 15 );

			~isMulticast = Mod ( Div ( $~nicAddress ; 1099511627776 ) ; 2 );

			$~hexNICAddress = "";

			$~base16 = "0123456789abcdef";

			$~step =

				If (

					~version < 3 //NIC is valid

					and $~nicAddress &#8804; 281474976710656

					and not ~isMulticast;

						1;

					/*else*/

						-1 //error

				)

		];

			UUIDGetNICAddress ( "" )

		);



	/*Convert NIC address from base 10 to base 16*/

	$~step = 1;

		Let ( [

			$~hexNICAddress =

				Middle ( $~base16 ; Mod ( $~nicAddress ; 16 ) + 1 ; 1 ) &

				$~hexNICAddress;

				$~nicAddress = Div ( $~nicAddress ; 16 );

				$~step = If ( $~nicAddress ; $~step ; /*else*/ $~step + 1 )

		];

			UUIDGetNICAddress ( "" )

		);



	/*Insert colons and return result*/

	$~step = 2;

		Let ( [

			~nicAddress = Right ( "000000000000" & $~hexNICAddress ; 12 );

			~nicAddress =

				Middle ( ~nicAddress ; 1 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 3 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 5 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 7 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 9 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 11 ; 2 );



			//purge variables

			$~nicAddress = "";

			$~hexNICAddress = "";

			$~base16 = "";

			$~i = "";

			$~step = ""

		];

			~nicAddress

		);



	/*Return error value*/

	$~step = -1;

		Let ( [

			//purge variables

			$~nicAddress = "";

			$~hexNICAddress = "";

			$~base16 = "";

			$~i = "";

			$~step = ""

		];

			"" //Null

		)

)

 

i am sorry , but i understand coding in an ide from many years ago. NOw you are teaching me something very new, which i would love to learn.

-ian

Os it just a bunch of "set Variable" statements . loops & calculations?

It's mostly Set Variable [] statements. The Case() becomes an If[]/Else If[] series, the Let()s become a series of Set Variable[], the $variables become $$variables, the ~letVariables become $variables, and the recursive custom function calls become recursive script calls followed by something using Get ( ScriptResult ) — Exit Script [Result: Get ( ScriptResult )] in the case of a tail recursive function.

thanks again Jeremy

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.