{"id":940,"date":"2011-10-03T14:18:23","date_gmt":"2011-10-03T18:18:23","guid":{"rendered":"http:\/\/mainlinetest.com\/?p=940"},"modified":"2011-10-03T14:31:08","modified_gmt":"2011-10-03T18:31:08","slug":"teststand-pass-by-value-vs-pass-by-reference","status":"publish","type":"post","link":"https:\/\/mainlinetest.com\/?p=940","title":{"rendered":"TestStand Pass By Value vs. Pass By Reference"},"content":{"rendered":"<p>In this example, I am going to demonstrate the difference of passing a variable into a subsequence by value versus passing by reference.<\/p>\n<p><em><strong>Pass By Reference<\/strong><\/em><\/p>\n<p>For those of you used to TestStand, you know that TestStand defaults to passing all variables into a subsequence by reference.\u00a0 This is useful considering some procedural languages actually require you to use pointers in order to pull this off.\u00a0 Passing by reference is also a more efficient use of memory as temporary copies of variables aren\u2019t being created, taking up space.\u00a0 However, users of TestStand need to be aware of this default setting because variables you are passing into the subsequence may be\u00a0 getting manipulated unintentionally.\u00a0 This might leave you debugging a test script for hours; not understanding why it keeps failing on you.\u00a0 Allow me to demonstrate\u2026<\/p>\n<p>Starting out with a new sequence, I am going to create two (2) local variables.\u00a0 The first variable I am going to make is a number and I will call it \u2018Year\u2019 and the second variable will be a string called \u2018Month\u2019.\u00a0 Next I will add a sequence in my sequence file and call it \u2018PassByRefandValueSequenceCall\u2019.\u00a0 Under the MainSequence I will now insert a Sequence Call step and have it call the \u2018PassByRefandValueSequenceCall\u2019 sequence.\u00a0 Finally, I will insert a Statement step into the MainSequence and initialize my two newly created variables.\u00a0 I am going to initialize \u2018Year\u2019 to 2011 and \u2018Month\u2019 to \u201cSeptember\u201d.\u00a0 Figure 1 below shows what this sequence currently looks like.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb.png\" border=\"0\" alt=\"image\" width=\"520\" height=\"189\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 1<\/strong><\/p>\n<p>The next thing I need to do in order to set up my example is I need to configure the \u2018PassByRefandValueSequenceCall\u2019 sequence to accept my two new variables \u2018Year\u2019 and \u2018Month\u2019.\u00a0 Therefore, within the \u2018PassByRefandValueSequenceCall\u2019 sequence, I am going to add two parameters; one number and one string.\u00a0 The first parameter I will call \u2018IntegerPassedfromMainSequence\u2019 and the second will be \u2018StringPassedfromMainSequence\u2019.\u00a0 Now I am going to add two Statement steps changing the values of the two parameters.\u00a0 As you can see in Figure 2 below, when this sequence call is made, \u2018Year\u2019 will be changed to 2012 and \u2018Month\u2019 will be changed to \u201cOctober\u201d.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb1.png\" border=\"0\" alt=\"image\" width=\"520\" height=\"240\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 2<\/strong><\/p>\n<p>Now that I have created my two parameters, I need to go back to the MainSequence and pass in my two local variables into \u2018PassByRefandValueSequenceCall\u2019.\u00a0 Figure 3 below shows how the Sequence Call was configured.\u00a0 Notice how both variables are being Passed by Reference.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb2.png\" border=\"0\" alt=\"image\" width=\"520\" height=\"207\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 3<\/strong><\/p>\n<p>So now what I want to do is show you what passing by reference means in TestStand.\u00a0 Remember, my sequence creates two variables \u2018Year\u2019 and \u2018Month\u2019, sets them to 2011 and \u201cSeptember\u201d, then passes them by reference to \u2018PassByRefandValueSequenceCall\u2019.\u00a0 Within this sequence, my script then changes \u2018Year\u2019 to 2012 and \u2018Month\u2019 to \u201cOctober\u201d before exiting.\u00a0 So what value are my variables once \u2018PassByRefandValueSequenceCall\u2019 is done executing and it returns back to the MainSequence?\u00a0 In order to demonstrate the answer I am going to insert some Message Boxes.\u00a0 I will add two message boxes inside \u2018PassByRefandValueSequenceCall\u2019 and two message boxes in the MainSequence after the sequence call is done executing.\u00a0 Figure 4 and 5 below show what my sequence now looks like.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image3.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb3.png\" border=\"0\" alt=\"image\" width=\"520\" height=\"173\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 4<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb4.png\" border=\"0\" alt=\"image\" width=\"520\" height=\"187\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 5<\/strong><\/p>\n<p>Now I am ready to execute my sequence and show what it means to pass a variable by reference.\u00a0 Figure 6 below shows the results.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image5.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb5.png\" border=\"0\" alt=\"image\" width=\"385\" height=\"163\" \/><\/a> <a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb6.png\" border=\"0\" alt=\"image\" width=\"388\" height=\"156\" \/><\/a><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb7.png\" border=\"0\" alt=\"image\" width=\"388\" height=\"156\" \/><\/a><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb8.png\" border=\"0\" alt=\"image\" width=\"396\" height=\"154\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 6<\/strong><\/p>\n<p style=\"text-align: left;\"><strong><\/strong>Are you amazed?\u00a0 Didn\u2019t think so.\u00a0 Look how after the two variables are manipulated inside the \u2018PassByRefandValueSequenceCall\u2019 the local variables in the MainSequence now show the same value.\u00a0 This makes sense since we are passing in a reference to the variables in memory and not some temporary copy.\u00a0 But what if you didn\u2019t want the sequence call to alter the values of the passed in variables?\u00a0 What if you intended the variables to be constants and didn\u2019t expect them to change?\u00a0 You could just insert a couple of Statement steps and save off copies of the variables into other temporary variables or you could just change the sequence call to accept the passed in variables by VALUE.<\/p>\n<p><em><strong>Pass By Value<\/strong><\/em><\/p>\n<p>Taking the existing script we created above, I am now going to change the parameters to be passed by value rather than reference.\u00a0 To do this in TestStand, you simply need to click on the parameter and uncheck \u2018Pass By Reference\u2019.\u00a0 Figure 7 below shows an example.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb9.png\" border=\"0\" alt=\"image\" width=\"319\" height=\"303\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 7<\/strong><\/p>\n<p>Now that both parameters have been configured to be passed by value, we are ready to execute our script again.\u00a0 But first, I want to go back to the MainSequence and show how the sequence call looks now.\u00a0 Figure 8 below now confirms that both parameters are indeed passed by value.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image10.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb10.png\" border=\"0\" alt=\"image\" width=\"520\" height=\"195\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 8<\/strong><\/p>\n<p>So now what happens whenever we execute the sequence?<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image11.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb11.png\" border=\"0\" alt=\"image\" width=\"380\" height=\"158\" \/><\/a><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image12.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb12.png\" border=\"0\" alt=\"image\" width=\"382\" height=\"154\" \/><\/a><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image13.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb13.png\" border=\"0\" alt=\"image\" width=\"378\" height=\"150\" \/><\/a><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image14.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2011\/10\/image_thumb14.png\" border=\"0\" alt=\"image\" width=\"376\" height=\"147\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 9<\/strong><\/p>\n<p>Figure 9 shows us how the original values of the local variables inside the MainSequence remain untouched.\u00a0 This is because the \u2018PassByRefandValueSequenceCall\u2019 did not alter the memory locations of the original variables but rather copies of them.\u00a0 One can see how this could be useful whenever variables being passed in are being treated as constants.\u00a0 Remember though that passing by reference is the preferred and more efficient method.\u00a0 Hence, NI defaults to it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, I am going to demonstrate the difference of passing a variable into a subsequence by value versus passing by reference. Pass By Reference For those of you used to TestStand, you know that TestStand defaults to passing all variables into a subsequence by reference.\u00a0 This is useful considering some procedural languages actually&nbsp;<a href=\"https:\/\/mainlinetest.com\/?p=940\" class=\"read-more\">Continue Reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":741,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[24,28,30,29,23],"class_list":["post-940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-national-instruments","tag-parameters","tag-pass-by-reference","tag-pass-by-value","tag-teststand","cat-1-id"],"_links":{"self":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts\/940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=940"}],"version-history":[{"count":11,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts\/940\/revisions"}],"predecessor-version":[{"id":949,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts\/940\/revisions\/949"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/media\/741"}],"wp:attachment":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}