Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Function changing values

$
0
0

When passing a variable to a function, when you modify the variable passed it also modifies the original variable. This isn't what I expect to happen. If I wanted to modify the original variable I would have just used the original variable name, or I would have used a return to pass it back.

Is there something I can do so that any manipulation I do inside the function is discarded unless I choose to use a return?

Example: I have this code written up in the ISE. I'd expect to see 1,2,3,4,5 on the screen. Not 5,2,3,4,5.

function test ($arr)

{

    $arr[0]=5

}

 

$array = 1,2,3,4,5

test $array

write-host "array" $array


Viewing all articles
Browse latest Browse all 6937

Trending Articles