# Passing a function as an objective function in JuMP

**URL:** https://discourse.julialang.org/t/passing-a-function-as-an-objective-function-in-jump/10745
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [May 6, 2018, 8:16pm UTC](https://discourse.julialang.org/t/passing-a-function-as-an-objective-function-in-jump/10745 "2018-05-06T20:16:30Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 6, 2018, 10:23pm UTC](https://discourse.julialang.org/t/passing-a-function-as-an-objective-function-in-jump/10745/2 "2018-05-06T22:23:22Z")

</div>

There is nothing stopping you from going

```julia
julia> using JuMP

julia> m=Model()
Feasibility problem with:
 * 0 linear constraints
 * 0 variables
Solver is default solver

julia> @variable(m, x)
x

julia> function foo(x)
       2x + 1
       end
foo (generic function with 1 method)

julia> @objective(m, Min, foo(x))
2 x + 1

julia> JuMP.getobjective(m)
2 x + 1

```

But, if you are building an objective with a large number of elements, this is going to be slow.

Is it really too much to use the macro? With a good data-structure and intendation, it is normally pretty easy to read. Alternatively, you could break it up into a few `@expression` objects…

```julia
julia> using JuMP

julia> m=Model()
Feasibility problem with:
 * 0 linear constraints
 * 0 variables
Solver is default solver

julia> @variable(m, x)
x

julia> @expression(m, foo, 2x + 1)
2 x + 1

julia> @objective(m, Min, foo)
2 x + 1

julia> JuMP.getobjective(m)
2 x + 1

```

---

_[View the full topic](https://discourse.julialang.org/t/passing-a-function-as-an-objective-function-in-jump/10745)._
