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? 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. Call us Mail us 21 September 2023 Qlik Sense Enterprise for Windows – Critical Security Fix #2 A new security issue in Qlik Sense Enterprise for Windows has been identified. This resolves an incomplete fix for the vulnerability that was reported earlier this month. Patches are available and it is recommended to update your Qlik Sense Enterprise for Windows environment at the earliest possibility. New Release Qlik Security 6 September 2023 Battle Ships & Big Data: join us for some fun! Unleash the Power of Data in the Ultimate Battle Ship Game! Join us at the Big Data Expo for a thrilling experience that blends strategy, competition, and analytics. Discover how data influences gameplay and be part of our research! Plus, dive into the world of data and analytics with the experts from Bitmetric. Don’t miss out! See you at booth 46! Bitmetric Event Qlik Team 29 August 2023 Qlik Sense Enterprise for Windows – Critical Security Fixes Two security issues in Qlik Sense Enterprise for Windows have been identified and patches made available. Details can be found in Security Bulletin Critical Security fixes for Qlik Sense Enterprise for Windows (CVE-2023-41266, CVE-2023-41265). New Release Qlik Security
21 September 2023 Qlik Sense Enterprise for Windows – Critical Security Fix #2 A new security issue in Qlik Sense Enterprise for Windows has been identified. This resolves an incomplete fix for the vulnerability that was reported earlier this month. Patches are available and it is recommended to update your Qlik Sense Enterprise for Windows environment at the earliest possibility. New Release Qlik Security
6 September 2023 Battle Ships & Big Data: join us for some fun! Unleash the Power of Data in the Ultimate Battle Ship Game! Join us at the Big Data Expo for a thrilling experience that blends strategy, competition, and analytics. Discover how data influences gameplay and be part of our research! Plus, dive into the world of data and analytics with the experts from Bitmetric. Don’t miss out! See you at booth 46! Bitmetric Event Qlik Team
29 August 2023 Qlik Sense Enterprise for Windows – Critical Security Fixes Two security issues in Qlik Sense Enterprise for Windows have been identified and patches made available. Details can be found in Security Bulletin Critical Security fixes for Qlik Sense Enterprise for Windows (CVE-2023-41266, CVE-2023-41265). New Release Qlik Security