13 October 2022 Qlik variable scope in subroutines Share this message 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: 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: 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: 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? 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: 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. 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? Want more? Then click the button below for our full archive! More Qlik test prep 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. Call us Mail us 16 March 2023 Qlik Sense Set Analysis with the P() and E() functions Learn how to answer questions like “What are our total sales for customer who’s purchases included X” by using implicit field selections with the p() and e() functions in Qlik Sense. Friday Qlik Test Prep Set Analysis Solution 9 March 2023 Column references in Qlik Sense This post demonstrates how column references in Qlik Sense work, and what considerations you should make regarding performance. Expressions Friday Qlik Test Prep Solution 7 March 2023 Visualizing a streak in Qlik Sense In this post you’ll learn how to visualize a streak in Qlik Sense based on a practical example with step by step instructyions. Functions Qlik
16 March 2023 Qlik Sense Set Analysis with the P() and E() functions Learn how to answer questions like “What are our total sales for customer who’s purchases included X” by using implicit field selections with the p() and e() functions in Qlik Sense. Friday Qlik Test Prep Set Analysis Solution
9 March 2023 Column references in Qlik Sense This post demonstrates how column references in Qlik Sense work, and what considerations you should make regarding performance. Expressions Friday Qlik Test Prep Solution
7 March 2023 Visualizing a streak in Qlik Sense In this post you’ll learn how to visualize a streak in Qlik Sense based on a practical example with step by step instructyions. Functions Qlik