So let's dive into these aforementioned evaluation strategies and try to understand the differences between them and hopefully clear up the confusion:
I want to use an analogy here. Let's consider soccer. In soccer there is one ball and players pass the ball to each other (to simply put it). If the ball so to say gets muddy, dirty and a player passes the ball to another player the second player will receive the muddy, dirty ball. And
let's assume the players decided to clean the ball because it got so dirty that it made it impossible play the game. When game continues with the clean ball none of the players will receive the old dirty, muddy ball they will all be playing with new clean and shiny ball.
This is pass by reference. The ball here is the reference. In pass by reference function receives a reference to the variable. If any change occurs in the reference every piece of code which uses that reference will be affected and will use the changed version of the reference.
Let's use another analogy here. Let's consider books. A person writes a book and publishes it. If you want to read that book you go to the bookstore and get yourself a copy of that book. You don't go to the author's house and grab the original writings. And if you decide to underline a sentence in your book the other books don't get the same sentence underlined, you only change your copy.
This is pass by value. The book here is the value. In pass by value function receives a copy of the value. If the function changes the value, the change will be limited in the function's scope.
Unfortunately I don't have an analogy for this evaluation strategy 😥 So I'm just going to write it as it is.
In call by sharing the function receives a copy of the reference to the object. If the object gets mutated, every piece of code which uses that object will use the changed version of the object.
By now the evaluation strategies playing part in the confusion and the differences they have should be clear which means we can skip to the reason behind the confusion part 🎉🎊 Let's consider the following code snippet:
john is passed to
confusionCreator(), actually the reference to
john is copied and passed to
confusionCreator(). Since there is a copying process it resembles pass by value.
name attribute is updated inside
confusionCreator() but affected the code pieces using
john outside the
confusionCreator() it resembles pass by reference.