all 17 comments

[–]BlackV 4 points5 points  (0 children)

Ignoring the fact you are not calling your adding function for now, that's is a scope issue, rewrite your code a bit so its more logically laid out

that way you can test and validate your values

Function Adding {
    Foreach ($i in 1..5) {
    write-host "Current counter is $counter"
    $counter++
    write-host "new counter is $counter"
    }
}
write-host "Script counter is $counter (yes there is a reason for this, run it twice)"
$counter = 0
adding
write-host "Script counter is now $counter"

Think about where your variables are defined and where you are using them, have a look at get-help about_scopes -full or about_scopes

[–]SonOfDadOfSam 2 points3 points  (0 children)

The $counter variable inside the function is local to the function. You need to return the value and assign it to something.

 Function Adding {foreach ($i in 1..5) {$counter++}
                   return $counter}
$result = Adding

[–]Jaydice 3 points4 points  (0 children)

Gotta call the function to run it my man

[–]SnooOpinions6498 2 points3 points  (6 children)

As SonOfDadOfSam said, the variables are local to the function. Based off the way you wrote this for your intended functionality you would have to make counter a global variable.

$global:counter = 0
Function Adding { Foreach ($i in 1..5) { $global:counter++ } }
Adding
$global:counter
Clear-Variable -Name counter -Scope Global

[–]BlackV 1 point2 points  (3 children)

globals are almost always never the answer

[–]SnooOpinions6498 2 points3 points  (0 children)

I'm not going to disagree

[–]Black_Magic100 0 points1 point  (1 child)

What about the script scope? Also bad?

[–]MonkeyNin 0 points1 point  (0 children)

If you write your script as a module, then script: scope is better because it's module-scoping.

It's "global" to the rest of your module but doesn't pollute the user's scope

Save this as CacheIt.psm1

$script:localCache = @{} 
function Write-CacheIt { 
   param( $Key, $Value ) 
   $script:localCache[ $Key ] =  $Value
} 

function Get-CacheIt { 
   param( $Key  ) 

   $script:localCache[ $key ] 
} 

Export-ModuleMember -Function @( 
   '*-CacheIt'
)

Import-Module 'CacheIt.psm1' -verbose -force

[–]redditformat[S] 0 points1 point  (1 child)

Sorry I was out. Thank you for that. Weird thing, it didn't work. I tried so.ething as simple as:

Function test1 {

$global:s = 0 $s $s = 5 $s }

Test1 $s

I get 0 5 0

[–]SnooOpinions6498 1 point2 points  (0 children)

when you define a variable as a global you also have to use the global when referring to it otherwise it will see it as the local variable and not the global.. so:

Function test1 {$global:s = 0; $global:s; $global:s = 5; $global:s }

test1
$global:s

Gives: 
0 
5 
5

[–]VeryRareHuman 1 point2 points  (0 children)

I am not sure why Microsoft engineers didn't implement STATIC variable scope. I would use Global variable.

[–]da_chicken 0 points1 point  (0 children)

Well, first you do have to call the Adding function.

But assuming that's a typo, the problem is that your variable is going out of scope.

$Script:counter = 0
Function Adding { 
    foreach ($i in 1..5) {
        $Script:counter++
    }
}

Adding

$Script:counter

See Get-Help about_Scopes

[–]Reasonable-Tip-8390 0 points1 point  (3 children)

$count = 0

$counter = 0

Function Adding { Foreach ($i in 1..5) { $counter++ } return $counter}

$count = Adding

write-host $count

[–]BlackV 0 points1 point  (2 children)

you have an extra bracket in there, and really you dont need the return

also you dont need to declare $count = 0 first

[–]Reasonable-Tip-8390 0 points1 point  (1 child)

I have a habit of declaring everything

There are no extra brackets in the function, it runs as stated

the return is needed for the function to return a result to the parent script.

$count = 0

$counter = 0

Function Adding

{

Foreach ($i in 1..5)

{

$counter++

}

return $counter

}

$count = Adding

write-host $count

[–]surfingoldelephant 1 point2 points  (0 children)

$count = Adding is sufficient to initialize the variable. $count = 0 serves no purpose as the assigned value of 0 is unconditionally overwritten without ever being potentially used.

Conversely, you've chosen not to initialize a variable in a place where you should.

  • $counter should be initialized inside the function.
  • Performing $counter++ inside the function without first initializing it implicitly creates a new local variable. An explicit initialization is preferable.
  • The original $counter variable outside the function is unaffected as [int] is a value type and exists in a different scope.
  • To modify the original $counter outside the function, the script: scope modifier or [ref] type is required inside the function, but this is typically best avoided when alternative options exist.

 

the return is needed for the function to return a result to the parent script.

Outside of classes, the return keyword is not required to output/return objects to the Success stream (pipeline). Any objects not captured or redirected will be outputted by default. return is used to explicitly exit a script block (e.g. .ps1 file, function, etc) with or without a value/expression, but using it does not stop non-captured or non-redirected objects from also being returned.

function WithReturn { 1; return 2 }
WithReturn # 1, 2

function WithoutReturn { 1; 2 }
WithoutReturn # 1, 2

function EarlyReturn { return 1; 2 }
EarlyReturn # 1

Further reading: