passing varaibles to function

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years, 5 months ago.

  • Author
    Posts
  • #11344
    Profile photo of wantto wantto
    wantto wantto
    Participant

    is the only way to pass variables to function is like this :

    function test {
    params([string]$arg1)
    Write-Host $arg1;
    }
    i understand i can't call
    test "dddd"+"eeeee"
    but only
    $str="dddd"+"eeeee"
    test $str

    isn't there another way to concat parms on the line i call the function?

  • #11348
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Your code has a type – it should be param not params

    Its not the only way but I think its the best especially when you start to look at advanced functions

    you could do

    test ("dddd"+"eeeee")

  • #11349
    Profile photo of wantto wantto
    wantto wantto
    Participant

    OK

  • #11350
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Yep, put parentheses around the expression to force PowerShell into Expression mode parsing, instead of Argument mode. See the about_Parsing help file for all the details on this.

You must be logged in to reply to this topic.