开发者

PowerShell Math Problem?

func开发者_如何学Ction other3($x, $y)
{
    $tmp = $x + $y
    return $tmp
}

$x = 5
$y = 10

$a = other3($x, $y)
Write-Host $a

Keeps returning 5 10 when it should be returning 15, what's the deal?


To call other3 with two parameters, drop the parenthesis "()" e.g.

$a = other3 $x  $y

The way you're currently calling it, actually passes one parameter, an array with two elements, i.e. 5 and 10. The second parameter is empty (probably defaults to null), meaning the addition does nothing and you simply return the $x parameter.


You're passing a list (5,10) to the parameter $x and $null to $y.

When the function adds $null to the list, you just get the list back.

Adding some write-host statements to the function should make this clear:

function other3($x, $y)
{
    $tmp = $x + $y
    write-host "`x=($x)"
    write-host "`y=($y)"
    return $tmp
}

$x = 5
$y = 10

$a = other3($x, $y)
Write-Host $a
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜