# Global constraints in functions

**URL:** https://discourse.julialang.org/t/global-constraints-in-functions/26341
**Category:** Optimization (Mathematical)
**Created:** [July 14, 2019, 12:45pm UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341 "2019-07-14T12:45:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)
#### Post date: [July 14, 2019, 12:45pm UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/1 "2019-07-14T12:45:26Z")

</div>

Hi,

I declared some constraints in a function because I needed an if condition, and wanted to know if I can somehow make these constraints defined outside the function as well. Here’s part of the function:

```julia
function amortization(z)
	if z == 0
          @constraints(m, begin
		       int[t = 1:T], γ[t] == k * ( δ0 - (t-1) * A )
		       pay[t = 1:T], δ[t] == A + γ[t]
           end)

```

So in this case, is there any way to edit this so that `int` and `pay` can be referred to, outside the function?

Thanks!

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [July 14, 2019, 1:24pm UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/2 "2019-07-14T13:24:27Z")

</div>

`return int, pay`?

---

<div class="post-metadata">

### Author: ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)
#### Post date: [July 15, 2019, 5:42am UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/3 "2019-07-15T05:42:00Z")

</div>

I’m getting an error

```julia
 UndefVarError: pay not defined

```

---

<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: [July 15, 2019, 3:31pm UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/4 "2019-07-15T15:31:30Z")

</div>

See the JuMP documentation:  
[http://www.juliaopt.org/JuMP.jl/v0.19.2/variables/#What-is-a-JuMP-variable?-1](http://www.juliaopt.org/JuMP.jl/v0.19.2/variables/#What-is-a-JuMP-variable?-1)  
[http://www.juliaopt.org/JuMP.jl/v0.19.2/constraints/](http://www.juliaopt.org/JuMP.jl/v0.19.2/constraints/)  
You can query named variables and constraints using a symbol.

You can do this:

```nohighlight
function add_variable(model)
    @variable(model, x)
end

function add_constraint(model)
    x = model[:x]
    @constraint(model, con, 2 * x <= 1)
end

model = Model()
add_variable(model)
add_constraint(model)
x = model[:x]
con = model[:con]

```

Or

```nohighlight
function add_constraint(model, x)
    @constraint(model, con, 2 * x <= 1)
    return con
end

model = Model()
@variable(model, x)
con = add_constraint(model, x)

```

---

<div class="post-metadata">

### Author: ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)
#### Post date: [July 16, 2019, 7:56am UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/5 "2019-07-16T07:56:49Z")

</div>

Thank you, that helped!

---

<div class="post-metadata">

### Author: ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)
#### Post date: [July 16, 2019, 12:39pm UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/6 "2019-07-16T12:39:41Z")

</div>

Just a quick question, would this method work for multiple constraints? Thanks

---

<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: [July 16, 2019, 1:37pm UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/7 "2019-07-16T13:37:13Z")

</div>

Yes.

```nohighlight
function foo(model)
    @variable(model, x[i=1:2])
    @constraint(model, con[i=1:2], x[i] <= 2)
end

model = Model()
foo(model)
x = model[:x]
con = model[:con]

x[1]
con[2]

```

---

<div class="post-metadata">

### Author: ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)
#### Post date: [July 17, 2019, 6:00am UTC](https://discourse.julialang.org/t/global-constraints-in-functions/26341/8 "2019-07-17T06:00:24Z")

</div>

Awesome, thanks!
