{"id":24292,"date":"2016-07-08T09:55:53","date_gmt":"2016-07-08T13:55:53","guid":{"rendered":"http:\/\/ok-cleek.com\/blogs\/?p=24292"},"modified":"2016-07-08T14:24:17","modified_gmt":"2016-07-08T18:24:17","slug":"vbscript","status":"publish","type":"post","link":"https:\/\/ok-cleek.com\/blogs\/?p=24292","title":{"rendered":"VBScript!"},"content":{"rendered":"<p>I've been fighting with a little script I wrote to massage some text for me. It's a very simple task so I thought VBS would be adequate (read each line of text, if there's a special bit of text in the line capitalize everything to its right - no big deal). I know I can use C# and JavaScript if I use Windows' Powershell, but I didn't think VBS would have any trouble with it. And it didn't. But I learned something ridiculous about VBS in the process.<\/p>\n<p>Here is a very simple VBScript program. <\/p>\n<blockquote><p>\n<code>Function timesTwo(ByRef inParam)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;inParam = inParam * 2<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;timesTwo = 1<br \/>\nEnd Function<\/p>\n<p>i = 10<\/p>\n<p>WScript.Echo \"i = \" & i<br \/>\ntimesTwo(i)<br \/>\nWScript.Echo \"i = \" & i<br \/>\nz = timesTwo(i)<br \/>\nWScript.Echo \"i = \" & i<\/code>\n<\/p><\/blockquote>\n<p>Here I have a simple function called 'timesTwo' that multiplies its input parameter by two and returns a value of 1. Since I declared the input of the function \"ByRef\", the input is passed \"by reference\", which basically means the function can change the value of the parameter. If I pass it a variable with a value of 5, the variable should have a value of ten when the function ends. Magic.<\/p>\n<p>If the parameter was declared \"ByVal\" (\"by value\"), the function would receive a <i>copy<\/i> of the input parameter, which it could modify as much as it wanted, but changing the copy wouldn't change the value of the original parameter. But I didn't declare it that way...<\/p>\n<p>The code below that function tests the function. It...<\/p>\n<ol>\n<li>initializes a variable called 'i' to a value of 10 (<code>i = 10<\/code>)\n<li>prints the value of i (<code>WScript.Echo \"i = \" & i<\/code> )\n<li>calls the function, ignoring its return value.  (<code>timesTwo(i)<\/code>)\n<li>prints the value of i\n<li>calls the function again, putting its return value in a variable 'z'. (<code>z = timesTwo(i)<\/code>)\n<li>prints the value of i\n<\/ol>\n<p>So, what do you think we should get?<\/p>\n<blockquote><p>\ni = 10<br \/>\ni = 20<br \/>\ni = 40\n<\/p><\/blockquote>\n<p>That seems reasonable. Start with <i>i<\/i> = 10, print <i>i<\/i>, call the function to double <i>i<\/i>, print <i>i<\/i>, call the function to double <i>i<\/i> again, print <i>i<\/i>.<\/p>\n<p>But if that worked, this would be a truly boring blog post!<\/p>\n<p>No, what we actually get is:<\/p>\n<blockquote><p>\ni = 10<br \/>\ni = 10<br \/>\ni = 20\n<\/p><\/blockquote>\n<p>Why? Well, I don't know exactly <i>why<\/i>. But, the effect is: if you ignore the return value of a function, the parameter you pass is actually passed by value (ByVal), not by reference. <\/p>\n<p>Pass by value:<\/p>\n<blockquote><p><code>timesTwo(i)<\/code><\/p><\/blockquote>\n<p>Pass by reference:<\/p>\n<blockquote><p><code>z = timesTwo(i)<\/code><\/p><\/blockquote>\n<p>What. The. Fuck. <\/p>\n<p>Why would that difference in behavior ever be useful, let alone expected ?<\/p>\n<p>I should've used JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I've been fighting with a little script I wrote to massage some text for me. It's a very simple task so I thought VBS would be adequate (read each line of text, if there's a special bit of text in the line capitalize everything to its right - no big deal). I know I can [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-24292","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/24292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24292"}],"version-history":[{"count":13,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/24292\/revisions"}],"predecessor-version":[{"id":24308,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/24292\/revisions\/24308"}],"wp:attachment":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}