# Adding a parameter to a function - how to think about dispatch?

**URL:** <https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892>\
**Category:** New to Julia\
**Created:** [August 24, 2021, 4:26am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892 "2021-08-24T04:26:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 24, 2021, 4:26am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/1 "2021-08-24T04:26:36Z")

</div>

I’m working my way through the 2021 Computational Thinking course. In the [transformations: II](https://computationalthinking.mit.edu/Spring21/transformations2/) - _Functions with parameters_ section

I’m not really understanding what I can do when parameterising a multiply-dispatched variant of a function.

Within Pluto I get a complaint that I’m creating multiple definitions of `f`. This may or may not be a Pluto problem. Anyway…

```julia
f(v) = [v[1]+v[2] , v[1]-v[2] ]
f(α) = ((x,y),) -> [x + αy, x - αy]

```

I was hoping that `f(3.0) ` would be a partial application of the `α` parameter and result in a single-Vector argumented function compatible with `f(v)`. It seems like a good story 🙂

Is there way of doing this - in the spirit of the two expressions above?

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 24, 2021, 4:40am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/2 "2021-08-24T04:40:41Z")

</div>

I don’t understand how the compiler is interpreting the parameters.

```julia
julia> f(v) = [v[1]+v[2] , v[1]-v[2] ]
f (generic function with 1 method)

julia> f(α) = ((x,y),) -> [x + αy, x - αy]
f (generic function with 1 method)

julia> g = f(2.0)
#1 (generic function with 1 method)

julia> g((5,7),)
ERROR: UndefVarError: αy not defined
Stacktrace:
 [1] (::var"#1#2")(::Tuple{Int64, Int64})
   @ Main ./REPL[3]:1
 [2] top-level scope
   @ REPL[7]:1

julia> typeof(g)
var"#1#2"

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [August 24, 2021, 5:12am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/3 "2021-08-24T05:12:02Z")

</div>

In your example, your second line does not define another method for `f`, but just redefines the first one, because `f(v)` and `f(α)` are both methods with the same number of arguments of any type. You have to give more specific argument types in at least one of the two definitions, for instance:

```julia
julia> f(v) = [v[1]+v[2] , v[1]-v[2] ]
f (generic function with 1 method)

julia> f(α::Number) = ((x,y),) -> [x + α*y, x - α*y]
f (generic function with 2 methods)

julia> g = f(2.0)
#1 (generic function with 1 method)

julia> g((5,7),)
2-element Vector{Float64}:
 19.0
 -9.0

```

Also, note in the error message that you got, that `αy` is not taken as the product of `α` times `y`, but as a single name (I’ve fixed also that in the example above).

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 24, 2021, 5:43am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/4 "2021-08-24T05:43:10Z")

</div>

> [@heliosdrm](#):
>
> ` f(α::Number) = ((x,y),) -> [x + α*y, x - α*y]`

Thanks so much.

In the original code in pluto, the \alpha was right next to the y, implying multiplication. Why did that work there? Actually it may just have been in markdown…

```julia
f(α) = ((x,y),) -> [x + αy, x - αy]

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [August 24, 2021, 7:37am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/5 "2021-08-24T07:37:17Z")

</div>

> [@banksiaboy](#):
>
> In the original code in pluto, the \alpha was right next to the y, implying multiplication. Why did that work there? Actually it may just have been in markdown…

That line of code is not actually executed in the notebook, so I do think it’s a mistake.

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 24, 2021, 10:20pm UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/6 "2021-08-24T22:20:55Z")

</div>

if i had looked down a few lines…

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 24, 2021, 10:35pm UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/7 "2021-08-24T22:35:54Z")

</div>

If `αy` were to mean multiplication, how would one go about creating a variable called `αy`? Any multi-character identifier would be ambiguous.

The reason `2x` can be interpreted as multiplication is that it isn’t a valid variable name.

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 25, 2021, 12:02am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-a-function-how-to-think-about-dispatch/66892/8 "2021-08-25T00:02:33Z")

</div>

Thanks - that’s good.  
I had made up a story if \alpha had been declared as a parameter it could be flagged as a variable. But I thought that might make a cranky parser.
