# For "sub" function, is this notation local?

**URL:** <https://forums.solar2d.com/t/for-sub-function-is-this-notation-local/300530>\
**Category:** Lua Language\
**Created:** [March 10, 2012, 2:58pm UTC](https://forums.solar2d.com/t/for-sub-function-is-this-notation-local/300530 "2012-03-10T14:58:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ignis075](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/d2c977/32.png) [@ignis075](https://forums.solar2d.com/u/ignis075)\
**Post date:** [March 10, 2012, 2:58pm UTC](https://forums.solar2d.com/t/for-sub-function-is-this-notation-local/300530/1 "2012-03-10T14:58:13Z")

</div>

Hi Lua veterans,  
I’m just curious, is the following “sub function” local in Lua scope? The function “TEST” itself isn’t declared as local, but it’s added as an entry (or sub-class?) of the table “sampleFunction”, which is defined as local.

```auto
local sampleFunction = {}  
  
function sampleFunction:TEST()  
 --function lines  
end  
```

So, by this notation, does the function “TEST” remain local, or does it remain global because it’s not \*itself\* declared as local? I’ve seen this format used in various codeshare modules, and I’m curious how the scoping on this works.

Thanks!  
Brent Sorrentino  
Ignis Design [import]uid: 9747 topic\_id: 23128 reply\_id: 323128[/import]

---

<div class="post-metadata">

**Author:** ![richard9](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@richard9](https://forums.solar2d.com/u/richard9)\
**Post date:** [March 10, 2012, 4:35pm UTC](https://forums.solar2d.com/t/for-sub-function-is-this-notation-local/300530/2 "2012-03-10T16:35:45Z")

</div>

Yeah, think of your function (“sampleFunction”) as a table. “TEST” is local because it is a nested component of that table/function. (As I understand it is addressable simply because your function is like a chunk of memory, holding the code inside, which is good if you need to call something again but bad if you load it up with different functions but only need one.) [import]uid: 41884 topic\_id: 23128 reply\_id: 92469[/import]

---

<div class="post-metadata">

**Author:** ![dejay.clayton](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dejay.clayton](https://forums.solar2d.com/u/dejay.clayton)\
**Post date:** [March 10, 2012, 7:50pm UTC](https://forums.solar2d.com/t/for-sub-function-is-this-notation-local/300530/3 "2012-03-10T19:50:03Z")

</div>

The way to understand this is to realize that the following syntax:

```auto
function sampleFunction:TEST( ... )  
 --function lines  
end  
```

is just convenient shorthand for the following equivalent notation:

```auto
sampleFunction.TEST = function( self, ... )  
 --function lines  
end  
```

That version makes it much easier to understand that the function being assigned is just an anonymous function that can only be accessed from the property to which it is bound, in this case, sampleFunction.TEST. [import]uid: 71767 topic\_id: 23128 reply\_id: 92486[/import]

---

<div class="post-metadata">

**Author:** ![ignis075](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/d2c977/32.png) [@ignis075](https://forums.solar2d.com/u/ignis075)\
**Post date:** [March 10, 2012, 8:42pm UTC](https://forums.solar2d.com/t/for-sub-function-is-this-notation-local/300530/4 "2012-03-10T20:42:19Z")

</div>

Thank you both! When it comes to Lua scopes, even an intermediate programmer like myself can’t always be certain, but this clears it up for me in this notation. [import]uid: 9747 topic\_id: 23128 reply\_id: 92492[/import]
