Search code examples
programming-languagesparametersargumentsreturn-value

passing arguments in different languages


can somebody please explain what is the difference between the following mechanisms of passing arguments: by value, by result, by result-value, with examples if possible, thanks in advance


Solution

  • For general info see Evaluation strategy.

    For example code you can check HERE.

    Also C# language specification can be useful :

    5.1.4 Value parameters

    A parameter declared without a ref or out modifier is a value parameter. A value parameter comes into existence upon invocation of the function member (method, instance constructor, accessor, or operator) or anonymous function to which the parameter belongs, and is initialized with the value of the argument given in the invocation. A value parameter normally ceases to exist upon return of the function member or anonymous function. However, if the value parameter is captured by an anonymous function (§7.15), its life time extends at least until the delegate or expression tree created from that anonymous function is eligible for garbage collection. For the purpose of definite assignment checking, a value parameter is considered initially assigned.

    5.1.5 Reference parameters

    A parameter declared with a ref modifier is a reference parameter. A reference parameter does not create a new storage location. Instead, a reference parameter represents the same storage location as the variable given as the argument in the function member or anonymous function invocation. Thus, the value of a reference parameter is always the same as the underlying variable. The following definite assignment rules apply to reference parameters. Note the different rules for output parameters described in §5.1.6.

    • A variable must be definitely assigned (§5.3) before it can be passed as a reference parameter in a function member or delegate invocation.

    • Within a function member or anonymous function, a reference parameter is considered initially assigned.

    Within an instance method or instance accessor of a struct type, the this keyword behaves exactly as a reference parameter of the struct type (§7.6.7).

    5.1.6 Output parameters

    A parameter declared with an out modifier is an output parameter. An output parameter does not create a new storage location. Instead, an output parameter represents the same storage location as the variable given as the argument in the function member or delegate invocation. Thus, the value of an output parameter is always the same as the underlying variable. The following definite assignment rules apply to output parameters. Note the different rules for reference parameters described in §5.1.5.

    • A variable need not be definitely assigned before it can be passed as
      an output parameter in a function
      member or delegate invocation.
    • Following the normal completion of a function member or delegate
      invocation, each variable that was
      passed as an output parameter is
      considered assigned in that execution path.
    • Within a function member or anonymous function, an output parameter is considered initially unassigned.
    • Every output parameter of a function member or anonymous function must be definitely assigned (§5.3) before the function member or anonymous function
      returns normally.

    Within an instance constructor of a struct type, the this keyword behaves exactly as an output parameter of the struct type (§7.6.7).