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);
CodePudding user response:
Let's change the script to make it more idiomatic and more clear (changed listed at the end):
function Test($paramA = 30, $paramB = 40)
{
$testC = $paramA ($paramB / $paramB) $paramA
"$testC"
}
$outerA = 4
$outerB = 3
$outerA
$outerB
$outerC
Test @($outerA, $outerB)
What happens is:
- The function is defined at the top.
$outerAand$outerBhave values assigned.$outerAand$outerBare named, and print their values, once per line.4and3.$outerCis named, it has not been used before and has no value (this is not an error in PowerShell), nothing is printed.- The function
Testis called with no named parameters and an array in first position. - The array is bound to
$paramA. - Nothing is bound to $paramB, so that takes the default value from the function header
$paramB = 40. $testC = $paramA ($paramB / $paramB) $paramAbecomes$testC = @(4,3) (40/40) @(4,3).- The middle bit resolves to 1, which makes
(@(4,3) 1) @(4, 3)which adds things onto the end of the first array and makes an array@(4, 3, 1, 4, 3). "$testC"is a string, using double quotes which means you can put variables in it and they get turned into text.- Putting an array into a string automatically joins the values in it with spaces, so
@(4, 3, 1, 4, 3)becomes the text"4 3 1 4 3" - Putting a value on its own in a function makes it part of the return value of the function, so this string comes out of
Test @($outerA, $outerB). - With nothing to capture the return value, it gets printed on the screen as well.
Code output:
4 # from outerA
3 # from outerB
4 3 1 4 3 # from outerA outerB (paramB/paramB) outerA outerB
30 is nowhere in the output because paramA is given a value, so the default is not used. 40 is nowhere in the output because 40/40 == 1.
Changes:
- Remove
;because it is only needed if you want to put many things on the same line. - Rename variables to separate the ones inside and outside the function.
- Rename the function so its name isn't part of the word "function".
- Change the values to make inside/outside function values more distinct.
- Put
()around the calculation to make order of precedence clearer, divide happens before add. - Put the optional
@in front of($outerA,$outerB)to make it more clear that's an array, not the function parameters. - Remove
echo, it is an alias forWrite-Outputand is the default thing which happens to variables if you just write their name. - Re-order the things, there's no reason for the function definition to be in the middle of the code.
CodePudding user response:
so the script initializes $a to be 4 and $b to be 3.\
$a = 4 ;
$b = 3 ;
the function needs $a and $b parameters that are set to be 4 and 3 by deafault.
then it calculates $c = $a $b / $b $a then prints the resault.
function fun ($a = 3 , $b = 4) {
$c = $a $b / $b $a;
echo "$c";
}
the script prints $a then $b then $c.
echo $a;
echo $b;
echo $c;
then the function fun is called.
fun ($a, $b);
output:
4
3
4 3 1 4 3
if you want the function fun to calculate %c probably you need to use
fun $a $b;
output:
4
3
9
