BluePro
BluePro

Reputation: 11

Output of a PowerShell script

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);

Upvotes: 0

Views: 190

Answers (2)

TessellatingHeckler
TessellatingHeckler

Reputation: 28963

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:

  1. The function is defined at the top.
  2. $outerA and $outerB have values assigned.
  3. $outerA and $outerB are named, and print their values, once per line. 4 and 3.
  4. $outerC is named, it has not been used before and has no value (this is not an error in PowerShell), nothing is printed.
  5. The function Test is called with no named parameters and an array in first position.
  6. The array is bound to $paramA.
  7. Nothing is bound to $paramB, so that takes the default value from the function header $paramB = 40.
  8. $testC = $paramA + ($paramB / $paramB) + $paramA becomes $testC = @(4,3) + (40/40) + @(4,3).
  9. 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).
  10. "$testC" is a string, using double quotes which means you can put variables in it and they get turned into text.
  11. 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"
  12. 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).
  13. 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 for Write-Output and 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.

Upvotes: 2

Cozer
Cozer

Reputation: 15

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

Upvotes: 0

Related Questions