Function Object - in PowerShell

In PowerShell

In the Windows PowerShell language, a script block is a collection of statements or expressions that can be used as a single unit. A script block can accept arguments and return values. A script block is an instance of a Microsoft .NET Framework type System.Management.Automation.ScriptBlock.

Function Get-Accumulator($x) { { param($y) return $script:x += $y }.GetNewClosure }

PS C:\> $a = Get-Accumulator 4

PS C:\> & $a 5

9

PS C:\> & $a 2

11

PS C:\> $b = Get-Accumulator 32

PS C:\> & $b 10

42

Read more about this topic:  Function Object