# How can you have a function that takes in values from the table it is in?

Ok, so lets say we have a table and a function is inside that table. This table contains alot of values, like this for example:

``````["a"] = {
b = false,
c = 1,
d = "e",
},
``````

And we also have a function as a value inside that table. (The function is defined on another place)

``````["a"] = {
b = false,
c = 1,
d = "e",
f = foo(b, c, d),
},
``````

How can you make the function have inputs from the table the function is in? (Like in the example)

rather than storing b,c and d, why not just put them in the function manually?

``````["a"] = {
f = foo(false, 1, "e"),
},
``````

unless you’re looking for a method in which to store the values in the table as well, perhaps:

``````local tbl = { -- first, store the values
["a"] = {
b = false,
c = 1,
d = "e",
},
}
tbl.a["f"] = foo(tbl.a.b, tbl.a.c, tbl.a.d) -- insert whatever foo returns at "f"
``````

This of course assumes a is stored within another table, index it as you need. Essentially what we’re doing is inserting the values b, c and d first, then calling `foo()` with the values of `tbl.a` as parameters, and inserting whatever `foo` returns into `tbl.a` with index `"f"`

Hope this helps

That’s not really what i asked…
What i want is that i DON’T have to individually give parameters. This makes my scripts cleaner and if the function isn’t the same on different tables, this method would suck. What i essentially mean is this:

``````["a"] = {
b = false,
c = 1,
d = "e",
f = function(parent) --Parent is the table itself
--Do something
end,
},
``````

WITHOUT having to manually input the table itself.

Forgive me for misunderstanding. I showed you some methods I believed would be applicable in your case.

I’m confused whether you’re intending to call the function from the table, or to store it there and call it later on. Regardless, as far as my knowledge goes, I’m not sure of any other methods to do what you’ve asked.

Hopefully someone else can help!

I store the function there and then call it later on. Sorry for being so unclear on my first post, i don’t know how to describe the issue im having.