# Function input vs argument

**URL:** <https://discourse.julialang.org/t/function-input-vs-argument/65040>\
**Category:** General Usage\
**Created:** [July 21, 2021, 3:28pm UTC](https://discourse.julialang.org/t/function-input-vs-argument/65040 "2021-07-21T15:28:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [July 21, 2021, 3:28pm UTC](https://discourse.julialang.org/t/function-input-vs-argument/65040/1 "2021-07-21T15:28:57Z")

</div>

Hi,

Sometimes it happens that I want to define a function `f(x,y)` that have two arguments (`x ,y` in this case ) to use it further in gradient computation or some similar application that requires a clear explicit definition of the function arguments (independent variables). However, this function `f` may also need some other inputs that are not arguments, they are needed just to complete some calculations inside the function `f`. My question is: how to pass these inputs without affecting the function call? I used to do it in a naïve inefficient way by simply make the other inputs global, so I can use them from inside, but I know that this is not the best thing to do.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 21, 2021, 3:48pm UTC](https://discourse.julialang.org/t/function-input-vs-argument/65040/2 "2021-07-21T15:48:03Z")

</div>

I would suggest that you (1) write the function to take _all_ of the arguments it actually needs and then (2) use closures to fix some subset of those arguments when you need to do something like compute a gradient.

For example:

```julia
julia> function f(x, y, z)
         z * (x + y)
       end
f (generic function with 2 methods)

julia> g = (x, y) -> f(x, y, 2)
#7 (generic function with 1 method)

julia> g(1, 2)
6

```

Here `f` takes three arguments, all the data it requires to perform its operations. But given some fixed `z` (2 in this case), we can create a new anonymous function of just `x` and `y` that calls `f` as needed.

In this way, all of the function arguments are clearly indicated and efficiently passed into `f`, but we can still use `g` as the two-argument function you’re looking for.
