我在函数#1 和触发函数#2 里面。函数#2 有一个变量,我想回到函数#1 并使用它。
我的输出最终是:
hey there
var is:
我想要的是输出:
var is: hey there
为什么我可以为函数提供一个变量,并且它使用它,但是当我在 #2 函数中更改该变量时,它不会更改它之后的变量returns
?
$var = $null
function one() {
two($var)
write-host "var is:" $var
}
function two($var){
$var = "hey there"
return($var)
}
clear
one
首先,更改您的two
函数以实际返回一个值:
function two {
$var = "hey there"
return $var
}
然后更新one
函数以通过将其分配给变量来实际“捕获”输出值:
function one {
# PowerShell doesn't use parentheses when calling functions
$var = two
Write-Host "var is:" $var
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句