Richard Siddaway's Blog

I had a question left on my blog regarding scope.  The following is a simplified version of the problem

functionlevel1{$iterations++Write-Host'$iterations in level 1 = '$iterationslevel2}functionlevel2{$iterations++Write-Host'$iterations in level 2 ='$iterations}$iterations=Write-Host'$iterations at start ='$iterationslevel1Write-Host'$iterations at end ='$iterations

Start with a variable $iterations. Call a function and increment the variable. Call a nested function and increment the variable and then display the result

The output is

$iterations at start =…
$iterations in level 1 =  1
$iterations in level 2 = 2
$iterations at end =…

Start with zero, increment in level1 and level 2 – all OK. Show the final result & its zero.

The reason is scope.  Each of the functions has its own scope. Those scopes are destroyed – along with any variables…

View original post 213 altre parole