# Why doesn't this table pass by reference? (SOLVED)

**URL:** <https://forum.defold.com/t/why-doesnt-this-table-pass-by-reference-solved/66911>\
**Category:** Questions\
**Created:** [November 27, 2020, 12:20pm UTC](https://forum.defold.com/t/why-doesnt-this-table-pass-by-reference-solved/66911 "2020-11-27T12:20:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Epitaph64](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/epitaph64/32/39288_2.png) [@Epitaph64](https://forum.defold.com/u/Epitaph64)\
**Post date:** [November 27, 2020, 12:20pm UTC](https://forum.defold.com/t/why-doesnt-this-table-pass-by-reference-solved/66911/1 "2020-11-27T12:20:51Z")

</div>

I’ve created a sample project here which demonstrates a Lua module where the table pass by reference is disregarded. See test.lua module. If I modify the function to return t2 and store it to t then the function behaves as expected. Can anyone explain this to me?

[My Project.zip](https://forum.defold.com/uploads/default/original/3X/d/7/d7b85dad00d5b60c67aaf5dbaf2363d5de538bf7.zip) (87.3 KB)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 27, 2020, 1:29pm UTC](https://forum.defold.com/t/why-doesnt-this-table-pass-by-reference-solved/66911/2 "2020-11-27T13:29:44Z")

</div>

To save everyone some time:

```lua
function scrub_tombstones(t, tombstone)
	local t2 = {}
	for k,v in pairs(t) do
		if v ~= tombstone then
			t2[k] = v
		else
			pprint("tombstone scrubbed")
		end
	end
	t = t2
end

t = {1, 2, 3, 4, 5}
scrub_tombstones(t, 3)
pprint(t)

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 27, 2020, 1:37pm UTC](https://forum.defold.com/t/why-doesnt-this-table-pass-by-reference-solved/66911/3 "2020-11-27T13:37:00Z")

</div>

This is a classic example of variable shadowing. The first argument “t” in scrub\_tombstones is local to the function and “shadows” the global variable “t”. When you do t = t2 you replace the local reference and not the global “t” declared outside the function.

This is also an example of why you should avoid using global variables (and functions!).

---

<div class="post-metadata">

**Author:** ![Epitaph64](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/epitaph64/32/39288_2.png) [@Epitaph64](https://forum.defold.com/u/Epitaph64)\
**Post date:** [November 27, 2020, 1:41pm UTC](https://forum.defold.com/t/why-doesnt-this-table-pass-by-reference-solved/66911/4 "2020-11-27T13:41:20Z")

</div>

Thank you, this is something I have to watch out for :blush luckily I have been testing things so I caught this before it became an issue. It’s one thing that bothers me slightly about Lua though for certain. I’ll try to write my code in a more functional style to avoid stuff like this.
