# Re: [Jprogramming] function within function; scope

```Dear Raul and Henry

thank you for your comprehensive list of ideas and explanation.
It helped.```
```
Ruda

March 20, 2020 11:22 PM, "Raul Miller" <rauldmil...@gmail.com> wrote:

> There are several ways to accomplish what I think you're asking for:
>
> Algebraic simplification:
> g=:3 :0
> -y
> )
>
> Pass the intermediate result as a parameter:
>
> g=:3 :0
> b=. 2*y
> f=. 4 :'y-x'
> b f y
> )
>
> Or
>
> g=:3 :0
> b=. 2*y
> f=. b 1 :'y-m'
> f y
> )
>
> Serialize the desired value and incorporate it in the definition of f
>
> g=:3 :0
> b=. 2*y
> f=. 3 :('y-',5!:5<'b')
> f y
> )
>
> Define f tacitly:
>
> g=:3 :0
> b=. 2*y
> f=. ] - b"_
> f y
> )
>
> If you want 'b' to be incorporated dynamically (so that updates
> propagate into f, then you should pass it as a parameter or pull a
> stunt like this:
>
> g=:3 :0
> b=. 2*y
> f=. ] - ".@'b'
> f y
> )
>
> You could also use locales, which might be handy if things get
> complex, but are way more complicated than this example seems to call
> for.
>
> g=:3 :0
> loc=. cocreate''
> b__loc=. 2*y
> f=. loc 1 :'y - b__m'
> r=. f y
> coerase loc
> r
> )
>
> There's other ways, also -- mostly variations on parameter passing.
>
> I hope this helps,
>
> --
> Raul
>
> On Fri, Mar 20, 2020 at 5:59 PM Rudolf Sykora <rsyk...@disroot.org> wrote:
>
>> Dear list,
>>
>> although I seem to be able to define a function within another function,
>> it seems that the inner function does not see private variables from the
>> outer one. What is then the correct way to write something like
>>
>> g =: 3 : 0
>> b =. 2*y
>> f =. 3 : 'y-b'
>> f y
>> )
>>
>> g 2
>>
>> and get -2 as a result?
>>
>> Here one can change b =. to b =: , but this makes b completely public,
>> and that's not particularly nice...
>>
>> Thanks
>> Ruda.
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
```