0

(I'm a PowerShell newbie, and finding it a very frustrating language)

I want to create a function - with arguments and a return value. Not rocket science? That's what I thought anyway. But PowerShell keeps concatenating my arguments - here's the MCVE version:

 function hello($a, $b) {
    Write-host "a is $a and b is $b"
 }

 hello("first", "second")

I was expecting this to produce the output:

 a is first and b is second

I wasn't expecting....

 a is first second and b is

along with the fact it is concatenating values in a way I don't expect, its also failing to flag up the argument which is consequently missing.

How can I pass arguments to a function? (!!!!OMG WTF????!!!)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • It's a shell, and you pass parameters like you do on the command line. Same way you wouldn't use `grep('pattern', 'file1')` but `grep 'pattern' file1`. They match so you can use (advanced) functions, cmdlets and native binary commands all in much the same way. – TessellatingHeckler Mar 19 '18 at 18:23
  • Well, in the OP's defense you would use parens for function arguments in some shells. Python for instance. Having said that, I would suggest that the OP type this into a powershell prompt: ***Get-Help functions*** then pay specific attention to the Advanced Functions topics. If you are going to learn powershell you might as well go straight to doing it in the most Best Practices compliant way possible right off the bat. – EBGreen Mar 19 '18 at 18:26

1 Answers1

2

Remove the () from your call to the function

function hello($a, $b) {
    Write-host "a is $a and b is $b"
 }

 hello "first" "second"
or
hello -a "first" -b "second"
Thom Schumacher
  • 1,469
  • 13
  • 24