Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:44:36+00:00 2026-05-29T09:44:36+00:00

Alright. I’m attempting to complete a school assignment and cannot for the life of

  • 0

Alright. I’m attempting to complete a school assignment and cannot for the life of me figure this out. I’m trying to use powershell to pass values from one function to another making a “modular” type script. I can’t seem to figure out how to move the values out of the scope of the function without using the $script:xxxxx. Is there another way to move the values in powershell as a regular argument parameter pass by reference?

Here’s what I have:

function main
{
inputGrams($carbGrams, $fatGrams)
$carbGrams
$carbGrams
calcGrams
displayInfo
}

function inputGrams([ref]$carbGrams, [ref]$fatGrams)
{
    $carbGrams = read-host "Enter the grams of carbs per day"
    $fatGrams = read-host "Enter the grams of fat per day"
}

function calcGrams
{
    $carbCal = $carbGrams * 4
    $fatCal = $fatGrams * 9
}

function displayInfo 
{
    write-host "The total amount of carb calories is $carbCal" 
    write-host "The total amount of fat calories is $fatCal"
}

main

The two values right after the inputGrams function should change each time the script is run but they don’t because of scope issues and passing the values. Anyone know how to properly pass those values back to the main function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:44:38+00:00Added an answer on May 29, 2026 at 9:44 am

    Andy is on the right path but [Ref] are tricky and it is recommended to avoid them if you can.

    As you said, the problem is scope. All your functions —except main— are called from main, therefore you need to make the variables available to these functions by setting them in main‘s scope, i.e. their parent scope, with Set-Variable or New-Variable.

    Same point is valid when retrieving their values with 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: I hope I did not spoil your school assignment, just wanted to help out 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm trying to figure out why I can't understand how to do this
Alright, here's what I'm trying to do. I'm attempting to write a quick build
Alright, I'm trying to use the basic document.getElementsByTagName function, but each time I do,
Alright, I am trying to accomplish this: When a user clicks a button that
Alright, I found out in this question that polling sockets does not scale, so
Alright. So I figure it's about time I get into unit testing, since everyone's
Alright, I'm trying to read a comma delimited file and then put that into
Alright, after doing a ton of research and trying almost every managed CPP Redist
Alright, I know how the fieldset / legend works out in HTML. Say you
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.