I am trying to understand passing parameters to functions in PowerShell and it shows different behavior while passing single/multiple parameters.
can someone explain why it is happening and what is correct way to do it?
Function Add-Numbers($Num1,$Num2){
return ($Num1 + $Num2);
}
Function SquareIt($Num){
return ($Num * $Num);
}
# only this adds two numbers correctly
$result1 = Add-Numbers 10 20
write-host "Result1: $result1";
#Passing single paramter works this way
$result2 = SquareIt(15)
write-host "Result2: $result2";
#Passing multiple numbers appends it rather than adding it
$result3 = Add-Numbers(10,20)
write-host "Result3: $result3";
Output:
Result1: 30
Result2: 225
Result3: 10 20