Энди на правильном пути, но [Ref] хитры, и рекомендуется избегать их, если вы можете .
Как вы сказали, проблема в объеме.Все ваши функции, кроме main , вызываются из main, поэтому вам нужно сделать переменные доступными для этих функций, установив их в области действия main , то есть в родительской области, с помощьюSet-Variable или New-Variable.
Та же точка действительна при получении их значений с помощью Get-Variable.
function main
{
inputGrams
$carbGrams
$fatGrams
calcGrams
displayInfo
}
function inputGrams
{
# type constrain as Single because Read-Host returns a String
[single]$carbs = read-host "Enter the grams of carbs per day"
[single]$fat = read-host "Enter the grams of fat per day"
# scope 1 is the parent scope, i.e. main's scope
Set-Variable -Name carbGrams -Value $carbs -Scope 1
Set-Variable -Name fatGrams -Value $fat -Scope 1
}
function calcGrams
{
# scope 1 is the parent scope, i.e. main's scope
Set-Variable -Name carbCal -Value ($carbGrams * 4) -Scope 1
Set-Variable -Name fatCal -Value ($fatGrams * 9) -Scope 1
}
function displayInfo
{
# scope 1 is the parent scope, i.e. main's scope
$_carbCal = Get-Variable -Name carbCal -Scope 1 -ValueOnly
$_fatCal = Get-Variable -Name fatCal -Scope 1 -ValueOnly
write-host "The total amount of carb calories is $_carbCal"
write-host "The total amount of fat calories is $_fatCal"
}
main
PS: надеюсь, я не испортил ваше школьное задание, просто хотелвыручить;)