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 😉 FAQ What are the implications of inadvertently creating global variables within a subroutine? Inadvertently creating global variables within a subroutine can lead to several challenges, especially in complex Qlik applications. When variables are created and not explicitly deleted before exiting a subroutine, they remain accessible in the global scope, which can cause naming conflicts with variables elsewhere in the script. This might lead to unexpected results, as scripts could unintentionally modify or use these global variables, leading to data integrity issues and debugging difficulties. Therefore, developers must be vigilant about managing variable scope and lifecycle to maintain script clarity and performance. How does the “stem” technique work for returning multiple values from a subroutine? The “stem” technique for returning multiple values from a subroutine involves using a naming convention that associates multiple related variables with a common prefix, allowing them to be treated as a grouped set of data. For example, if a subroutine calculates several metrics, each metric could be stored in a separate variable with names like Metric.Sales, Metric.Profit, and Metric.Revenue. This method simplifies the process of returning and accessing multiple related values from a subroutine, as they can be easily identified and manipulated based on their common prefix. We’ll go more in-depth on the practical implementation in a future blog post. How can developers effectively manage and clean up variables within a subroutine to prevent clutter in the global scope? Effective management and cleanup of variables within a subroutine to prevent clutter in the global scope can be achieved through disciplined coding practices and the strategic use of Qlik’s scripting capabilities. Developers should adopt a clear naming convention for variables that are meant to be temporary or local to a subroutine, making it easier to identify and manage these variables. One approach is to prefix temporary variables with a specific string, such as “temp_”, which signals that these variables should not persist beyond their immediate use. Additionally, developers can leverage the LET and SET commands to explicitly delete or nullify these variables at the end of a subroutine. This ensures that temporary variables do not remain in memory and affect the global scope, thereby preventing potential conflicts or memory issues. It’s also advisable to document the purpose and expected lifespan of each variable within the script, aiding in maintenance and readability. For subroutines that are called frequently or generate a significant number of temporary variables, developers might consider implementing a cleanup routine or a section at the end of the subroutine dedicated to variable management. This routine can systematically remove or reset all temporary variables, ensuring that the global scope remains clean and that the application’s performance is not adversely affected. Incorporating these practices into the development workflow not only helps in managing the variable scope effectively but also contributes to a more structured, understandable, and maintainable Qlik script. By being mindful of the scope and lifecycle of variables, developers can mitigate risks associated with global variable pollution and ensure that their Qlik applications remain robust and efficient. That’s it for this week. See you next Friday? More from the Bitmetric team Qlik Cloud Backup Protect your investment in Qlik with daily incremental backups stored in an encrypted environment with redundant storage. Available for as little as 2 Euro per day. Learn more. Masters Summit for Qlik The Masters Summit for Qlik provides the next step in your path to becoming a Qlik specialist. As an extra perk, Bitmetric has secured an over $450 discount just for you! Use code BITMETRIC at checkout. Read more here. Friday Qlik Test Prep Script Solution How can we help? Barry has over 20 years experience as a Data & Analytics architect, developer, trainer and author. He will gladly help you with any questions you may have. Call us Mail us 13 June 2025 Qlik Extension: Scroll to Object Ever since Qlik introduced extended sheets in June 2018, we at Bitmetric have been fans. The ability to extend a sheet vertically and add visualizations below the fold is a powerful way to present more insights without cluttering the main view. Yet, even after seven years, it still feels like a hidden feature. Many users […] Extension Qlik 2 June 2025 Simplifying Data Integration with TimeXtender At Bitmetric, we believe in making technology work for people, not the other way around. TimeXtender’s approach to data integration fits this philosophy by removing obstacles and giving users practical tools to manage their data. Solution TimeXtender 12 May 2025 Dagelijkse Qlik Ondersteuning: altijd hulp bij data analyse Bij Bitmetric snappen we hoe belangrijk het is dat je Qlik omgeving gewoon goed werkt. Of je nu dagelijks data analyseert of snel wat inzichten nodig hebt, je wilt dat alles soepel verloopt. Daarom staan onze experts elke dag voor je klaar. Van kleine vragen tot ingewikkelde uitdagingen, we helpen je graag verder, zodat jij je kunt focussen op wat echt telt! Qlik
13 June 2025 Qlik Extension: Scroll to Object Ever since Qlik introduced extended sheets in June 2018, we at Bitmetric have been fans. The ability to extend a sheet vertically and add visualizations below the fold is a powerful way to present more insights without cluttering the main view. Yet, even after seven years, it still feels like a hidden feature. Many users […] Extension Qlik
2 June 2025 Simplifying Data Integration with TimeXtender At Bitmetric, we believe in making technology work for people, not the other way around. TimeXtender’s approach to data integration fits this philosophy by removing obstacles and giving users practical tools to manage their data. Solution TimeXtender
12 May 2025 Dagelijkse Qlik Ondersteuning: altijd hulp bij data analyse Bij Bitmetric snappen we hoe belangrijk het is dat je Qlik omgeving gewoon goed werkt. Of je nu dagelijks data analyseert of snel wat inzichten nodig hebt, je wilt dat alles soepel verloopt. Daarom staan onze experts elke dag voor je klaar. Van kleine vragen tot ingewikkelde uitdagingen, we helpen je graag verder, zodat jij je kunt focussen op wat echt telt! Qlik