<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Subroutine-local variables in Qlik script in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1553255#M639885</link>
    <description>I ran your version of the script, and the post-CALL TRACE statement failed to print any number i.e. it cleaned-up the variable as expected. My suspicion is that when you were playing around with this technique, you accidentally created a global variable. So, go to Settings &amp;gt; Variable Overview..., explicitly remove the variable, and try running again.</description>
    <pubDate>Wed, 06 Mar 2019 22:28:42 GMT</pubDate>
    <dc:creator>gussfish</dc:creator>
    <dc:date>2019-03-06T22:28:42Z</dc:date>
    <item>
      <title>Subroutine-local variables in Qlik script</title>
      <link>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1091992#M639883</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For all my fellow software engineers who have yearned for being able to declare local variables in Qlik script subroutines, I have found a workaround for achieving this: declare them as parameters in the SUB's parameter list; like this:&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14593032575366718 jive_text_macro" jivemacro_uid="_14593032575366718" modifiedtitle="true"&gt;
&lt;P&gt;/* SUB printTriangularNumber (pN) - print the (pN)th triangular number&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Outputs pN x (pN+1) / 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @param pN&lt;/P&gt;
&lt;P&gt;*/&lt;/P&gt;
&lt;P&gt;SUB printTriangularNumber (pN, /* local variables: */ lTriangularNumber )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LET lTriangularNumber=(pN+1)*pN/2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TRACE $(lTriangularNumber);&lt;/P&gt;
&lt;P&gt;END SUB&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;CALL printTriangularNumber( (5) );&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;Notes:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The CALL has fewer parameters than the SUB declaration, but the QlikView engine nevertheless matches the two and initialises the missing parameters (lTriangularNumber, in this case) to null().&lt;/LI&gt;&lt;LI&gt;Style: I've used variable prefixes (p and l) and an inline comment to distinguish the genuine parameters from the local variables in the subroutine's signature; but have omitted the local variables in the comment block explaining the subroutine.&amp;nbsp; This makes it as easy as possible for the maintainer to understand what is going on.&lt;/LI&gt;&lt;LI&gt;Like any other SUB parameter, there is no need to "clean-up" lTriangularNumber with a "SET lTriangularNumber=;" statement - the variable will be automatically deleted on return from the subroutine.&lt;/LI&gt;&lt;LI&gt;EXCEPTION: this technique does not work if there are no genuine parameters for the subroutine (i.e. the subroutine's signature contains &lt;EM&gt;only&lt;/EM&gt; 'local' variables or you call it with no parameters: in these cases, the QlikView engine will create the variables at global scope and not dispose of them when it reaches ENDSUB.&amp;nbsp; (Add that to the case file of Qlik's poor language design decisions that make it harder to easily create defect-free code.)&lt;/LI&gt;&lt;LI&gt;Qlik script behaves correctly when a SUB with a given parameter is called from a SUB that has a parameter with exactly the same name: it will indeed treat the outer subroutine's parameter and the inner subroutine's namesake parameter as two distinct variables, even if it's a recursive call.&lt;/LI&gt;&lt;LI&gt;I've put extra parentheses around the '5' in the CALL to force the QlikView engine to treat this as an expression.&amp;nbsp; Without this, the QlikView engine will create a variable named '5' (yes, really!). (See previous comment about language design).&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: Angus Monro - 
* added Exception dot point re this technique not working when the subroutine's CALL passes no parameters; 
* altered the recommended style - previous I'd demarcated the local variables in the SUB signature with an inline comment both before and after the list of local variables; but having a comment at the end of the list is redundant, since all variables through to the end of the signature will be 'local' when this technique is used.
* added a couple of self-indulgent rants about Qlik's language design. It's an ongoing issue and I wish they'd take a community-consultative approach so that impacts of design choices can be assessed.  Argh!  There I go again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Mar 2016 02:15:29 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1091992#M639883</guid>
      <dc:creator>gussfish</dc:creator>
      <dc:date>2016-03-30T02:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: Subroutine-local variables in Qlik script</title>
      <link>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1091993#M639884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;SUB printTriangularNumber(pN,lTriangularNumber)&lt;/P&gt;&lt;P&gt;LET lTriangularNumber = (pN+1)*(pN/2);&lt;/P&gt;&lt;P&gt;TRACE $(lTriangularNumber);&lt;/P&gt;&lt;P&gt;ENDSUB &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;CALL printTriangularNumber((5));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;TRACE Accessing out of &amp;gt;End sub&amp;lt; $(lTriangularNumber);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Capture.PNG" class="jive-image image-1" src="https://community.qlik.com/legacyfs/online/171090_Capture.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am able to accessing SUB local variable as globally...As you said above the local variable will be automatically deleted on return from the subroutine....&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 22 Jul 2017 21:04:02 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1091993#M639884</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-07-22T21:04:02Z</dc:date>
    </item>
    <item>
      <title>Re: Subroutine-local variables in Qlik script</title>
      <link>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1553255#M639885</link>
      <description>I ran your version of the script, and the post-CALL TRACE statement failed to print any number i.e. it cleaned-up the variable as expected. My suspicion is that when you were playing around with this technique, you accidentally created a global variable. So, go to Settings &amp;gt; Variable Overview..., explicitly remove the variable, and try running again.</description>
      <pubDate>Wed, 06 Mar 2019 22:28:42 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1553255#M639885</guid>
      <dc:creator>gussfish</dc:creator>
      <dc:date>2019-03-06T22:28:42Z</dc:date>
    </item>
    <item>
      <title>Re: Subroutine-local variables in Qlik script</title>
      <link>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1553260#M639886</link>
      <description>Post-scriptum: I've just noticed that if $(#var) notation is used with a "local" variable, the variable ceases to be local.&lt;BR /&gt;Actually, $(#var) notation is even weirder than this - it looks like the Qlik script engine finds all instances of this notation and creates the variables, and does all this before it has executed a single line of the script! To see this effect, run this script:&lt;BR /&gt;exit script;&lt;BR /&gt;trace $(#ghostvar);&lt;BR /&gt;Obviously, the trace statement should never run. But if you execute this bit of script and then check Settings &amp;gt; Variable Overview..., you'll find that there is now a variable named 'ghostvar'! Bizarre.</description>
      <pubDate>Wed, 06 Mar 2019 22:36:30 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Subroutine-local-variables-in-Qlik-script/m-p/1553260#M639886</guid>
      <dc:creator>gussfish</dc:creator>
      <dc:date>2019-03-06T22:36:30Z</dc:date>
    </item>
  </channel>
</rss>

