Home > Mobile >  Difference between pass-by-value and pass-by-reference?
Difference between pass-by-value and pass-by-reference?

Time:02-25

I'm new here, I'm learning the programming language Go. I previously departed from an object-oriented programming language, so I'm a bit confused when it comes to pass-by-value and pass-by-reference topics

Please help to explain the difference between the two and examples of each programming language that uses the method. Thank You

CodePudding user response:

If you pass something by value, you're passing the value the object represents. In contrast, if you pass something by reference, you're passing a reference to the object itself.

For example, take this sample variable a:

var a = "test"

Passing a by value means passing "test". If you modify what you passed, it will not affect a.
However, if you pass a by reference, you're passing a. This means that what you passed is a reference to the original variable a directly, and modifying it modifies a as well.

  • Related