Sathish
Sathish

Reputation: 237

Powershell addition not working

I am trying a simple powershell program. But for some reason its not working. Can someone help me with this?

Ex:-

Script:-

function Add-Numbers($a,$b) {
    return $a + $b
}
Add-Numbers $a $b

When I try to run this on the powershell cmd, I dont get any output.

But if I modify this a little bit (as below), it works fine.

Script:-

param($a,$b)

function Add-Numbers($a,$b) 
{
    return $a + $b
}
Add-Numbers $a $b

Upvotes: 0

Views: 240

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You have to call the Add-Numbers with some actual values:

function Add-Numbers($a,$b) {
    $a + $b
}
Add-Numbers 1 2

Will return 3.

If you want to execute the ps1 and pass values to it, you have write the Param block in the first line:

param($a,$b)

function Add-Numbers($a,$b) 
{
    $a + $b
}
Add-Numbers $a $b

Now you can invoke the script with two values. Note: You will invoke the ps1 file, not the name of the function you defined (the whole script gets executed).

Upvotes: 1

Related Questions