locked
confused about when to use function{ param(a,b)} vs function( a, b){} RRS feed

  • Question

  • Hi,

    I'm kind of confused/lost when to use one form vs the other when defining parameters in a function.

    f.ex.

    1) param() defined in the function

    function get-total-1() {
        param([int] $a, [int] $b)
        return $a+$b
    };

    2) param as a function signature 

    function get-total-2([int] $a, [int] $b){
        return $a+$b
    };

    When should you use get-total-1 vs get-total-2 parameter form in a function?

    Or is there any reason to prioritize one form vs the other ?

    Thanks a lot for helping me clarifying this (problem).

    Kind regards,

    Didier


    • Edited by ddalu Sunday, February 3, 2019 4:56 PM
    Sunday, February 3, 2019 9:41 AM

Answers

  • The second one is mostly not used except when we do quickie functions.  Bot serve the same purpose.

    With "Param" we get to easily add attributes.


    \_(ツ)_/

    • Proposed as answer by Proed Sunday, February 3, 2019 11:30 AM
    • Marked as answer by ddalu Sunday, February 3, 2019 4:55 PM
    Sunday, February 3, 2019 10:09 AM