13 October 2022

Qlik variable scope in subroutines

Share this message
Qlik variable scope in subroutines - Bitmetric Friday Qlik Test Prep question

Every Friday at Bitmetric we’re posting a new Qlik certification practice question to our LinkedIn company page. Last Friday we asked the following Qlik Data Architect certification practice question about Qlik variable scope in subroutines:

Qlik variable scope in subroutines question of the week.

Many people got the right answer, though not always for the right reason. There’s a few things to unpack here.

The correct answer is A: null

As we’ve seen in the question about passing by reference and by value from two weeks ago, when calling the subroutine on line 8, we supply 2 as the value to pass to the pInput parameter. Or so we think! The result isn’t what we expected though. When we check the variable overview we now suddenly have a variable called 2 with a value of 4:

Qlik Sense variable overview

So what’s happening here?

We’re still passing by reference

When passing an argument to a parameter, we have the following options:

  • Passing a string, for example ‘Hello world‘. This will pass as value.
  • Passing an expression, for example 1+1. We can also pass a fixed value as an expression by enclosing it in parentheses, for example (2). This will pass as value.
  • Passing a variable, for example vNumber. This will pass as reference.

Note that there is no option to pass an integer. It looks as if we’re passing the value 2 on line 7, but in reality we’re still passing a reference. In this case to a variable called 2. This variable does not yet exist, but gets created on line 3. What happens is that the statement gets interpreted as:

Qlik Sense variable LET

So, the result of the multiplication gets set to the variable named 2, not the parameter named pInput. When tracing pInput on line 8 it returns null.

So if we fix the input it would print?

So what if we change line 7 to use (2) instead of 2. Will that make the TRACE statement on line 8 work?

Qlik pass variable as value

No!

We are now passing the value the right way to the subroutine. Alternatively, we could also have used ‘2’ (string) or even 1+1 (also an expression).

On the bright side, the variable called 2 is no longer being created so we’ve improved the situation somewhat. The TRACE statement still returns null though.

Can we even get this to print?

No, passing by value or by reference was a red herring, we can never get it to trace in this setup.

pInput is a parameter of the MultiplyByTwo subroutine. Subroutine parameters are always scoped to the subroutine they belong to. This means that, outside of the subroutine, pInput does not exist. Even if we set the right value, we still won’t be able to reference it on line 8.

How can we pass values back from a subroutine then?

There are multiple ways to return values from a subroutine:

Providing a variable by reference as an argument

As we learned in the question from two weeks ago, whe can provide a variable by reference as an argument. The contents of that variable can be updated by the subroutine and referenced outside of the subroutine:

Qlik pass variable by reference

Creating a variable within the subroutine

While parameters are scoped to the subroutine, any variables that are created within the subroutine will exist in the global scope. In the following example we can reference vResult outside of the subroutine.

Returning values from a Qlik subroutine

It’s also good to note that if you want variables to only exist within the subroutine you should delete them before exiting the subroutine.

Return multiple values with the “stem” technique

When you want to return multiple values from a subroutine, the “stem” technique is a clever way to go about it. We’ll discuss this in another blog post. If you want to learn more sooner, then you may want to join us for the Masters Summit for Qlik, where Rob Wunderlich covers this in his Advanced Scripting training 😉

That’s it for this week. See you next Friday?

Masters Summit for Qlik. Advanced Qlik training

Take your Qlik skills to the next level!

If you enjoy Qlik, you’ll love the Masters Summit for Qlik. 3 days of Qlik deep dives, peer networking, ready to use resources and much more! See you there!

Test your Qlik knowledge!

Check out the Friday Qlik Test Prep archive for more Qlik questions and answers.

Friday Qlik Test Prep Script Solution

How can we help?

Feel free to contact us if you have any comments or questions.