how do I increment an integer variable I passed into a function in Scala?

后端 未结 5 1421
深忆病人
深忆病人 2021-01-18 05:34

I declared a variable outside the function like this:

var s: Int = 0

passed it such as this:

def function(s: Int):         


        
5条回答
  •  无人共我
    2021-01-18 06:09

    You don't.

    A var is a name that refers to a reference which might be changed. When you call a function, you pass the reference itself, and a new name gets bound to it.

    So, to change what reference the name points to, you need a reference to whatever contains the name. If it is an object, that's easy enough. If it is a local variable, then it is not possible.

    See also call by reference, though I don't think this question is a true duplicate.

提交回复
热议问题