# How to access a function inside a function in extern module?

**URL:** <https://forums.solar2d.com/t/how-to-access-a-function-inside-a-function-in-extern-module/326724>\
**Category:** Lua Language\
**Created:** [July 2, 2014, 2:01pm UTC](https://forums.solar2d.com/t/how-to-access-a-function-inside-a-function-in-extern-module/326724 "2014-07-02T14:01:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![d.mach](https://forums.solar2d.com/user_avatar/forums.solar2d.com/d.mach/32/58_2.png) [@d.mach](https://forums.solar2d.com/u/d.mach)\
**Post date:** [July 2, 2014, 2:01pm UTC](https://forums.solar2d.com/t/how-to-access-a-function-inside-a-function-in-extern-module/326724/1 "2014-07-02T14:01:37Z")

</div>

I thought this one is an easy one, but now I’m trying to access this for about an hour and can’t figure it out.

Here is my problem:

I have an external module, like this one…

```
local M = {} local innerfunction -- functions are now local: local testFunction1 = function() &nbsp;&nbsp;&nbsp; print( "Test 1" ) &nbsp;&nbsp;&nbsp; -- now the function I want to access later &nbsp;&nbsp;&nbsp; innerfunction = function (event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- my inner function code &nbsp;&nbsp;&nbsp; end -- assign a reference to the above local function M.testFunction1 = testFunction1 M.innerfunction = innerfunction -- Finally, return the table to be used locally elsewhere return M

```

Now I want to access not only testFunction1 which I can easily by doing this…

```
local example = require "examplemodule" example.testFunction1() -- prints "Test 1" to terminal

```

Instead I want to access innerfunction directly, like for example with a Runtime listener in the main code.

How can this be done?

My first guess was to do this:

```
local example = require "examplemodule" Runtime:addEventListener ("touch",example.innerfunction)

```

But with this code I get an error.

Is there a way to access this kind of ‘nested’ functions in a module?

Any help welcome!

Best,

Daniela

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [July 2, 2014, 8:29pm UTC](https://forums.solar2d.com/t/how-to-access-a-function-inside-a-function-in-extern-module/326724/2 "2014-07-02T20:29:10Z")

</div>

Daniela,

Hi. &nbsp;You’re confusing the concept of references and pointers. &nbsp;Lua does not have a pointer variable types. &nbsp;All variables contain either scalar values or references to other objects (tables).

At this point in your code,&nbsp;

```
-- assign a reference to the above local function M.testFunction1 = testFunction1 M.innerfunction = innerfunction

```

The variable (from higher in the code)&nbsp;

```
local innerfunction

```

&nbsp;contains ‘nil’. &nbsp;So, when the module is loaded, you are assigning ‘nil’ to ‘M.innerfunction’

Later, when you run the function ‘testFunction1()’ the variable ‘innerfunction’ is assigned a new value. &nbsp;This however has no affect on&nbsp;‘M.innerfunction’.

It is also very important to understand, that EVERY time you run&nbsp;‘testFunction1()’, a new function is created and assigned to&nbsp;‘innerfunction’, which is pretty wasteful.

I’m not sure why you’re trying this, regardless, hopefully the following code will help you understand this concept:

```
-- Place this code in daniela.lua local m = {} local innerFunc local count = 0 local outerFunc = function() count = count + 1 print("\n--------------------") print( "outerFunc called " .. count .. "times") print( "BEFORE: innerFunc == " .. tostring( innerFunc ) ) -- Creates NEW copy of function every time. m.innerFunc = function() print("Do something") end print( "AFTER: innerFunc == " .. tostring( innerFunc ) ) end m.outerFunc = outerFunc return m

```

Now test the above module:

```
local daniela = require "daniela" print(daniela.outerFunc) print(daniela.innerFunc) daniela.outerFunc() daniela.outerFunc() print(daniela.outerFunc) print(daniela.innerFunc)

```

Question: Are you trying to achieve an OOP like hierarchy, including private and public data?

---

<div class="post-metadata">

**Author:** ![d.mach](https://forums.solar2d.com/user_avatar/forums.solar2d.com/d.mach/32/58_2.png) [@d.mach](https://forums.solar2d.com/u/d.mach)\
**Post date:** [July 2, 2014, 8:39pm UTC](https://forums.solar2d.com/t/how-to-access-a-function-inside-a-function-in-extern-module/326724/3 "2014-07-02T20:39:06Z")

</div>

Thank you (again) for your fast help! Now things aren’t so confusing anymore 🙂

I shouldn’t do so much ‘learning by doing’ with lua I guess. Maybe I should continue reading the language handbooks. 😉

Daniela

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [July 2, 2014, 8:29pm UTC](https://forums.solar2d.com/t/how-to-access-a-function-inside-a-function-in-extern-module/326724/4 "2014-07-02T20:29:10Z")

</div>

Daniela,

Hi. &nbsp;You’re confusing the concept of references and pointers. &nbsp;Lua does not have a pointer variable types. &nbsp;All variables contain either scalar values or references to other objects (tables).

At this point in your code,&nbsp;

```
-- assign a reference to the above local function M.testFunction1 = testFunction1 M.innerfunction = innerfunction

```

The variable (from higher in the code)&nbsp;

```
local innerfunction

```

&nbsp;contains ‘nil’. &nbsp;So, when the module is loaded, you are assigning ‘nil’ to ‘M.innerfunction’

Later, when you run the function ‘testFunction1()’ the variable ‘innerfunction’ is assigned a new value. &nbsp;This however has no affect on&nbsp;‘M.innerfunction’.

It is also very important to understand, that EVERY time you run&nbsp;‘testFunction1()’, a new function is created and assigned to&nbsp;‘innerfunction’, which is pretty wasteful.

I’m not sure why you’re trying this, regardless, hopefully the following code will help you understand this concept:

```
-- Place this code in daniela.lua local m = {} local innerFunc local count = 0 local outerFunc = function() count = count + 1 print("\n--------------------") print( "outerFunc called " .. count .. "times") print( "BEFORE: innerFunc == " .. tostring( innerFunc ) ) -- Creates NEW copy of function every time. m.innerFunc = function() print("Do something") end print( "AFTER: innerFunc == " .. tostring( innerFunc ) ) end m.outerFunc = outerFunc return m

```

Now test the above module:

```
local daniela = require "daniela" print(daniela.outerFunc) print(daniela.innerFunc) daniela.outerFunc() daniela.outerFunc() print(daniela.outerFunc) print(daniela.innerFunc)

```

Question: Are you trying to achieve an OOP like hierarchy, including private and public data?

---

<div class="post-metadata">

**Author:** ![d.mach](https://forums.solar2d.com/user_avatar/forums.solar2d.com/d.mach/32/58_2.png) [@d.mach](https://forums.solar2d.com/u/d.mach)\
**Post date:** [July 2, 2014, 8:39pm UTC](https://forums.solar2d.com/t/how-to-access-a-function-inside-a-function-in-extern-module/326724/5 "2014-07-02T20:39:06Z")

</div>

Thank you (again) for your fast help! Now things aren’t so confusing anymore 🙂

I shouldn’t do so much ‘learning by doing’ with lua I guess. Maybe I should continue reading the language handbooks. 😉

Daniela
