Values Passed By Reference

Hello everyone, I noticed that variables and inputs can be “Passed By Reference”, but what does this mean? I also noticed that a macro like “Decrement Int” inputs a value by reference, what does this accomplish, and what is the difference between it being by reference, and not?

When something is passed by value, it is copied and sent to the operation. This copy is slow, and it can actually be a huge performance hit if you’re copying big things and a lot of them, often.

When something is passed by reference, the instruction is given the original’s memory address. No copy is required, but you lose all control over it. It can be read later, overwrote, erased, or whatever else it wants. If anything was changed, it happened to the original source.

Alright, this sounds like something I will be using. I will be making a function that “encrypts” any variable that is inputted into the function. You mentioned that the copy is slow, but then also mentioned that no copy is required, so will a function that just inputs a variable by reference, and multiplies it be a huge performance hit when doing it often? What exactly do you mean when you say that it copies the operation?

No. I wasn’t clear enough.

There are two different things you can pass: a copy of the value or the memory location (reference).

Passing by reference: “Where is your data?”
Passing by value (aka: not passing by reference): “What is your data?”


Think of it like this… pretend an object has a list: “1, 2, 3, 4, 5”.
You have a function that is designed to reverse a list of numbers.

Case 1: If your function receives a value (NOT a reference)
If you ask the object, “what is your list”?

To do this, it needed to create a new list, which is probably a chunk of the program’s local memory where temporary stuff is held.
It then needed to read the first value of the first list, which is “1”, and place it in the first value of the second list.
It then needed to read the second value of the first list, which is “2”, and place it in the second value of the second list.
If then needed to read the third value of the first list, which is “3”, and place it in the third value of the second list.
If then needed to read the fourth value of the first list, which is “4”, and place it in the fourth value of the second list.
If then needed to read the fifth value of the first list, which is “5”, and place it in the fifth value of the second list.
It then needs to point your function to the second list.

Your function then does its multi-step process to reverse it.
Your function now has a chunk of memory with “5, 4, 3, 2, 1” in it. It can store it, or it will be deleted when the function ends. We’ll pretend it stores it somewhere.
The object still has its original chunk of memory with “1, 2, 3, 4, 5” in it.

Case 2: If your function receives a reference (memory location)
If you ask the object “where is your list”?
The object says “It’s over here.”
Your function receives a 32- or 64-bit memory location to the start of the list “1, 2, 3, 4, 5” on the object. It can store that 32- or 64-bit address, or it will be deleted when the function ends. We’ll pretend it stores it somewhere.
Your function then does its multi-step process to reverse it.
Your function now has a 32- or 64-bit reference to the object’s list’s memory location with “5, 4, 3, 2, 1” in it.
That memory is the object’s memory, though. The object’s list is now “5, 4, 3, 2, 1”.

Later on, something else tells the original object to change the third value to “6”.

In Case 1: (Above)
The object’s list is now: “1, 2, 6, 4, 5”.
Your function’s stored list is still: “5, 4, 3, 2, 1”.

In Case 2: (Above)
The object’s list is now: “5, 4, 6, 2, 1”.
Your function’s has a stored 32- or 64-bit reference to the object’s list’s memory location, which is now: “5, 4, 6, 2, 1” (because it’s the list in the line above this).

That big copy in the first part of Case 1 did a lot of work, but now the function has its own, unique copy of the data. It also cannot modify the original data, because it’s hidden away in the object’s memory… and the object didn’t tell you where that is. This also means that, if you want the original object’s list to change, you need to ask the object to do it. You can’t just do it for it. Again, you don’t know where in memory that is (unless you were given a reference).

Awesome, that was an amazing explanation. I really appreciate you taking the time to give such a detailed answer.

No problem. Note that I’ve tweaked its wording a bit. Check the original post for the most clear verbiage.