I'm seeing some unexpected behavior, think it has something to do with a variable being passed as a reference vs as a value.
Is this right?
Do you know of a way to be explicit passing something as a value?
Intended behavior:
Function takes an array, returns an array of the inputted array in reverse order.
When using an array, the function modifies the array in the parent scope ($originalList).
When using an arraylist, the function does not modify the arraylist in the parent scope ($originalList).
the doco is a but unclear about how which types are passed https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ref?view=powershell-7
[array]$originalList = 1..5
function wtf {
[CmdletBinding()]
param (
[parameter(Mandatory = $true)][array]$tempList
)
process {
[array]::Reverse($tempList)
return ($tempList)
}
}
Write-Host " before" -ForegroundColor Cyan
Write-Host '$originalList'
Write-Host $originalList
Write-Host
Write-Host ' function returns' -ForegroundColor Cyan
wtf -tempList $originalList
Write-Host
Write-Host " after" -ForegroundColor Cyan
Write-Host '$originalList'
Write-Host $originalList
Output
before
$originalList
1 2 3 4 5
function returns
5
4
3
2
1
after
$originalList
5 4 3 2 1