# Calling a nested function from other file to a constructor

**URL:** https://discourse.julialang.org/t/calling-a-nested-function-from-other-file-to-a-constructor/31590
**Category:** General Usage
**Created:** [November 27, 2019, 4:54pm UTC](https://discourse.julialang.org/t/calling-a-nested-function-from-other-file-to-a-constructor/31590 "2019-11-27T16:54:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [November 27, 2019, 4:54pm UTC](https://discourse.julialang.org/t/calling-a-nested-function-from-other-file-to-a-constructor/31590/1 "2019-11-27T16:54:10Z")

</div>

let us suppose that we have:

```julia
include("file2.jl")
struct f1
    function f1():
         expressions

       function f2(a,b,c):
                return g2(a)
       end
end
end

```

where `g2()` is a nested function in a function called `g1()`but in another file called `file2` like this:

```julia
function g1()
      function g2(b)
      return expression
      end
end

```

, how can i call `f2`i have tried this:

```julia
a=f1()
a.f2(x,y,z)

```

but i got an `ERROR: UndefVarError: g2 not defined`

---

<div class="post-metadata">

### Author: ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)
#### Post date: [November 27, 2019, 6:56pm UTC](https://discourse.julialang.org/t/calling-a-nested-function-from-other-file-to-a-constructor/31590/2 "2019-11-27T18:56:49Z")

</div>

Function `g1` in `file2` returns a function object. To use it you have to call `g1()` in your function `f1` or `f2`

```julia
include("file2.jl")
struct f1
    function f1()
        # expressions
        ftc = g1()
        
        function f2(a,b,c)
            return ftc(a)
        end
    end
end

```

or

```julia
include("file2.jl")
struct f1
    function f1()
        # expressions
        function f2(a,b,c)
            ftc = g1()
            return ftc(a)
        end
    end
end

```

Then you can write:

```julia
a = f1() # calls the constructor returning the a callable object
a(3, 2, 1)

```

(Drop the colons in your function definitions 😉 In general it is easier to answer questions if you post real code instead of pseudo-code.

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [November 29, 2019, 1:44pm UTC](https://discourse.julialang.org/t/calling-a-nested-function-from-other-file-to-a-constructor/31590/3 "2019-11-29T13:44:39Z")

</div>

i forgot to drop the colons because i’m coming from a Python background 😅  
but your idea is working

---

<div class="post-metadata">

### Author: ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)
#### Post date: [November 29, 2019, 5:15pm UTC](https://discourse.julialang.org/t/calling-a-nested-function-from-other-file-to-a-constructor/31590/4 "2019-11-29T17:15:05Z")

</div>

Ja, your approach looks object oriented. That made me wonder, how to define a method as an element of an object with access to other elements. Here is one way to achieve this:

```julia
mutable struct Omf
    f
    p::Int

    function Omf(p)
        omf = new()
        omf.p = p
        function fd(x)
            return omf.p * x
        end
        omf.f = fd
        return omf
    end
end

julia> omf = Omf(1)
Omf(var"#fd#3"{Omf}(Omf(#= circular reference @-2 =#)), 1)

julia> omf.f(5)
5

julia> omf.p = 2
2

julia> omf.f(5)
10

julia> omf.p = 3
3

julia> omf.f(5)
15

```

However, I think programming julia is fun because of multiple dispatch. Maybe you want to have a look at [Stefan’s talk at JuliaCon 2019](https://youtu.be/kc9HwsxE1OY).
