I am new to powershell and i am trying to understand the output of this script.
Can someone please provide an explanation of the output?
$a = 4 ;
$b = 3 ;
function fun ($a = 3 , $b = 4)
{
$c = $a + $b / $b + $a;
echo "$c";
}
echo $a;
echo $b;
echo $c;
fun($a,$b);
fun($a,$b)
is not how you call functions in PowerShell; it needs to befun $a $b
. The way you have it will pass an array into the first param and use the default for the second param. (Unhelpfully, you've named everything the same name, that won't help make it clear what's happening).fun -a @($a, $b) -b $null
so inside your function$a
is@(4, 3)
and$b
is4
($null
defaults to4
) sofun($a, $b)
is calculating@(4, 3) + (4 / 4) + @(4, 3)
and outputting the array@(4, 3, 1, 4, 3)
as4 3 1 4 3
(it's not doing numeric math - it's doing array append operations). Note the/
takes precedence over+
so$b / $b
is1
.