2

I get a little bit confused on this topic since in c-code the local scope is anything between curly braces, and you can nest one local scope inside another local scope and they are all invisible to each other if you define them at a lower level.

But, in PowerShell, where are the local scope boundaries?

POWERSHELL

function myfun {
   param([boolean]$yesno)

   if ($yesno) {
       $result = "foo"
   }
   else {
      $result = "bar"
   }
   write-host "$result" # foo or bar?  where does a  local scope begin end?? is 
                        # is local scope always the start of a function? and 
                        # and all variables can be seen no matter how many curly
                        # braces are in the function?
}

Versus C code:

void myfun(int yesno) {
    if (yesno) {
        const char* result = "foo";
    }
    else {
        const char* result = "bar";
    }
    printf("%s\n", result); // ERROR! result does not exist
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bill Moore
  • 165
  • 4
  • 1
    `$result` is defined in the scope of your function `myfun` hence you can call that variable inside it without problem, however if you call the same variable on the script scope (outside the function) you wouldn't see it defined there. There are different scopes and scope modifiers in powershell, the best read is [about_Scopes](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2) – Santiago Squarzon May 22 '22 at 15:22
  • Technically, you could consider everything between `myfun { ... }` as "the boundary", however, parameters (those in the `param` block) can also have [attribute declarations](https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/parameter-attribute-declaration?view=powershell-7.2) such as `ValidateScript`, `ValidateRange`, etc.. and what happens inside them is "another boundary" too – Santiago Squarzon May 22 '22 at 15:34
  • 2
    In general, functions (by default) and script (blocks) that are executed using the `&` call operator begin a new scope. The topic is complex, there is propably already a comprehensive answer by [mklement0](https://stackoverflow.com/users/45375/mklement0). Let's search... – zett42 May 22 '22 at 15:45
  • 2
    https://stackoverflow.com/questions/36346817/powershell-private-scope-seems-not-useful-at-all/36347515#36347515 has some good information and [about Scopes](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Scopes?view=powershell-7.2) as well. Especially try to understand what this excerpt from mklement0 means: _"As most shells do, PowerShell uses dynamic rather than lexical scoping."_. If you can explain why this prints "42", "42" instead of "42", "21", you have basically understood it: `$x = 42; & { Write-Host $x; $x = 21 }; Write-Host $x` – zett42 May 22 '22 at 15:53
  • 2
    Good call @zett42, mklement0's answer covers it very well – Santiago Squarzon May 22 '22 at 16:22

0 Answers0