# Accessing Internal variables in a function

**URL:** <https://discourse.julialang.org/t/accessing-internal-variables-in-a-function/65194>\
**Category:** New to Julia\
**Created:** [July 23, 2021, 4:50pm UTC](https://discourse.julialang.org/t/accessing-internal-variables-in-a-function/65194 "2021-07-23T16:50:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amit1/32/25318_2.png) [@amit1](https://discourse.julialang.org/u/amit1)\
**Post date:** [July 23, 2021, 4:50pm UTC](https://discourse.julialang.org/t/accessing-internal-variables-in-a-function/65194/1 "2021-07-23T16:50:56Z")

</div>

I came across following snippet in Flux tutorial:

```julia
function linear(in, out)
  W = randn(out, in)
  b = randn(out)
  x -> W * x .+ b
end

linear1 = linear(5, 3) # we can access linear1.W etc
linear1.W

```

Here `linear1.W` can print the value of W. I am bit confused regarding the exact nature of linear1. Are internals of the function accessible in julia?

My own attempt proved futile:

```julia
function add_22_2_xy(x,y)
     a = 22
     return a + x + y
end

ex1 = add_22_2_xy

ex1.a

```

Above snippet returns

> type #add\_22\_2\_xy has no field a

Can someone clarify when internals of a function can be accessed?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 23, 2021, 4:57pm UTC](https://discourse.julialang.org/t/accessing-internal-variables-in-a-function/65194/2 "2021-07-23T16:57:49Z")

</div>

Your linear function returns an anonymous function, but your add\_22\_2\_xy function returns a number. Also, in the first example you call the linear function, and in the second you just assign the add\_22\_2\_xy function to a variable.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 23, 2021, 5:06pm UTC](https://discourse.julialang.org/t/accessing-internal-variables-in-a-function/65194/3 "2021-07-23T17:06:39Z")

</div>

I think it’s something specific to closure. As the [documentation](https://docs.julialang.org/en/v1/devdocs/functions/#Closures) says:

> A closure is simply a callable object with field names corresponding to captured variables. For example, the following code:
> 
> ```julia
> function adder(x)
> return y->x+y
> end
> 
> ```
> 
> is lowered to (roughly):
> 
> ```julia
> struct ##1{T}
> x::T
> end
> 
> (_::##1)(y) = _.x + y
> 
> function adder(x)
> return ##1(x)
> end
> 
> ```

So because the `W` is captured by the closure, you can access it like a field.
