# Passing \`limits\` as function arguments

**URL:** https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777
**Category:** New to Julia
**Tags:** question
**Created:** [August 20, 2018, 5:29pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777 "2018-08-20T17:29:43Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [August 20, 2018, 5:29pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/1 "2018-08-20T17:29:43Z")

</div>

I’m tryin to build a function that take 2 inputs `limits` and `Function`, so I wrote the below:

```julia
function Σ(limits, f::Function)
    @show limits
    @show f
end

Σ((i=1:3), :(x->x^2))

```

But it could not run, with the below error:

> ERROR: LoadError: function Σ does not accept keyword arguments

I did not understand shall I change something in the function declaration, or in the function execution?

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [August 20, 2018, 5:52pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/2 "2018-08-20T17:52:04Z")

</div>

`Σ((i=1:3,), x->x^2)`? It depends what you’re trying to do. `:(x->x^2)` is a quoted expression, not a function.

---

<div class="post-metadata">

### Author: ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)
#### Post date: [August 20, 2018, 5:53pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/3 "2018-08-20T17:53:59Z")

</div>

The `typeof` function is your friend here.

```julia
julia> typeof(:(x->x^2))
Expr

julia> :(x->x^2) isa Function
false

julia> typeof((i=1:3))
ERROR: function typeof does not accept keyword arguments
Stacktrace:
 [1] kwfunc(::Any) at ./boot.jl:321
 [2] top-level scope at none:0

julia> typeof((i=1:3,))
NamedTuple{(:i,),Tuple{UnitRange{Int64}}}

```

As such, to call your function you need to write it as

```julia
Σ((i=1:3,), x->x^2)

```

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [August 20, 2018, 6:59pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/4 "2018-08-20T18:59:30Z")

</div>

Thanks, but how can I read the limits range in my function!

I want to get something like:

```julia
start = limit.first
stop = limits.last

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [August 20, 2018, 7:07pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/5 "2018-08-20T19:07:44Z")

</div>

Then you can pass a `Pair`:

```julia
julia> limits = 1 => 3
1 => 3

julia> limits.first
1

julia> limits.second
3

```

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [August 20, 2018, 7:10pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/6 "2018-08-20T19:10:05Z")

</div>

> [@fredrikekre](#):
>
> limits.first

☹  
Got this error:

> ERROR: LoadError: type NamedTuple has no field first

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [August 20, 2018, 7:13pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/7 "2018-08-20T19:13:28Z")

</div>

Then you didn’t use a pair. Can you show your code instead of making people guessing what you have?

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [August 20, 2018, 7:28pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/8 "2018-08-20T19:28:48Z")

</div>

Here it is:

```julia
function Σ(limits, f::Function)
    @show limits
    out = 0.0
    start = limits.first
    stop = limits.second

    for i=start:stop
        out += f(i)
    end
    out
end

result = Σ((i=1:6,), n->(6n + 2))
@show result

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [August 20, 2018, 7:34pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/9 "2018-08-20T19:34:04Z")

</div>

You are passing a named tuple and not a Pair as I suggested. Change `(i = 1:6,)` to `1 => 6`:

```julia
julia> function Σ(limits, f::Function)
           out = 0.0
           start = limits.first
           stop = limits.second
           for i = start:stop
               out += f(i)
           end
           out
       end
Σ (generic function with 1 method)

julia> Σ(1=>6, n->(6n + 2))
138.0

```

But if you just use `start` and `stop` to recreate the range `start:stop` in the function, then maybe it makes more sense to just use the range, that is, pass `1:6` and remove `start` and `stop`:

```julia
julia> function Σ(range, f::Function)
           out = 0.0
           for i = range
               out += f(i)
           end
           out
       end
Σ (generic function with 1 method)

julia> Σ(1:6, n->(6n + 2))
138.0

```

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [August 20, 2018, 7:37pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/10 "2018-08-20T19:37:12Z")

</div>

Thanks a lot, I loved the solution of using `range`  
I re-wrote it as:

```julia
function Σ(range, f::Function)
    out = 0.0
    (for i = range; out += f(i) ; end)
    out
end

result = Σ(1:6, n->(6n + 2))
@show result

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [August 20, 2018, 8:02pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/11 "2018-08-20T20:02:50Z")

</div>

If you don’t mind relying on the already existing `sum` function, this is also an option:

```julia
Σ(range, f::Function) = sum(f(i) for i in range)

```

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [August 20, 2018, 8:13pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/12 "2018-08-20T20:13:28Z")

</div>

Supreme, very silky solution, thanks 🤗

So, Apparently I can avoid adding my function, by using the below

```julia
f(n) = 6n + 2
result = sum(f(n) for n=1:6)
@show result

```

---

<div class="post-metadata">

### Author: ![platawiec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/platawiec/32/31914_2.png) [@platawiec](https://discourse.julialang.org/u/platawiec)
#### Post date: [August 20, 2018, 8:49pm UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/13 "2018-08-20T20:49:33Z")

</div>

Here’s the slickest way (you can pass the function you are summing with as the first argument):

```julia
julia> f(n) = 6n + 2
f (generic function with 1 method)

julia> sum(f, 1:6)
138

```

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [August 21, 2018, 5:51am UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/14 "2018-08-21T05:51:16Z")

</div>

By the way, with my [Reduce.jl](https://github.com/chakravala/Reduce.jl) package, you can do this

```nohighlight
julia> using Reduce
Reduce (Free CSL version, revision 4590), 11-May-18 ...

julia> @force using Reduce.Algebra

julia> ∑(:(x^2),:x,1,3)
14

julia> ∏(:(x^2),:x,1,3)
36

```

it takes summation limits and symbolic expressions as arguments, of course it’s not as fast as using a function.

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [August 21, 2018, 7:54am UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/15 "2018-08-21T07:54:14Z")

</div>

Thanks, the link is not working.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [October 23, 2020, 4:39am UTC](https://discourse.julialang.org/t/passing-limits-as-function-arguments/13777/16 "2020-10-23T04:39:40Z")

</div>

They work for me. I’m not sure I fully understand the documentation though.
