The title is explicit. What fundamental or fundamental difference is there between these methods when defining the functions or methods?
Important: Despite being a very similar question to this one on stackoverflow , it is not intended to be a translation of any of their answers.
When it is by value , the information of the variable is stored in a different memory address when received in the function, therefore if the value of that variable changes it does not affect the original variable, it is only modified within the context of the function.
When it is by reference , the variable that is received as a parameter in the function points to exactly the same memory address as the original variable, so if its value is modified within the function, the original variable is also modified.
As explained in this gif.
The essence of the difference corresponds to the freedom (or restriction) that exists on the parameter of the function or method.
Passing by value refers to passing a copy of the parameter value from the client function/method. Being a copy, the changes made directly in the function/method on the value of the parameter will not be reflected at the end of the execution of the function/method.
Passing by reference refers to passing the same parameter value from the client function/method. Being the same value, the changes made directly in the function/method on the value of the parameter will be reflected at the end of the execution of the function/method.
To give an example of this, imagine a physical document that you have in your hands. You pass this document by value when you first make a copy of the document and hand it over to a colleague. If the partner, by some accident, stains this document, your original document will not be affected since the partner has worked on a copy. On the other hand, passing the document by reference means that you give the original document to your colleague, and if he burns it, creates a new one with totally different content and returns it to you, then now what he has given you will be the document and the original document was lost :(.
More technically, you can see this example in C:
Printing result:
It is important to know if the programming language used allows the passing of variables by value and/or by reference. For example, Java and Scala do not allow variables to be passed by reference. C, C++, C#, Visual Basic, among others, do allow both types of variable passing.
Important: Reference passing should not be confused with reference value passing. The latter means that a copy of the reference is passed, however it allows editing the state of the reference referenced by the parameter (forgive the redundancy).