Before understanding the 2 terms, you MUST understand the following. Every object, has 2 things that can make it be distinguished.
- Its value.
- Its address.
So if you say employee.name = "John"
know that there are 2 things about name
. Its value which is "John"
and also its location in the memory which is some hexadecimal number maybe like this: 0x7fd5d258dd00
.
Depending on the language's architecture or the type (class, struct, etc.) of your object, you would be either transferring "John"
or 0x7fd5d258dd00
Passing "John"
is known as passing by value.
Passing 0x7fd5d258dd00
is known as passing by reference. Anyone who is pointing to this memory location will have access to the value of "John"
.
For more on this, I recommend you to read about dereferencing a pointer and also why choose struct (value type) over class (reference type)