Beginner Question: How to use JuMP model variables in different functions

I want to create a JUMP energy system model, where different features can be added or not.
So my idea was to use different functions which can be called if needed for adding new equations to the model. Cannot figure out what the problem is.

Here simple code with an error:

using JuMP

function add_storage!(m::Model, Vol_m3::Int64, time_steps::OrdinalRange{Int64,Int64})
    @variable(m, M3_storage[time_steps]>=0)
    @constraint(m, storageCap[t in time_steps], M3_storage[t]<=Vol_m3)
    return m


With the print below I see all the variables I wanted.


Subject to
 storageCap[1] : M3_storage[1] <= 200.0
 storageCap[2] : M3_storage[2] <= 200.0
 storageCap[3] : M3_storage[3] <= 200.0
 storageCap[4] : M3_storage[4] <= 200.0
 M3_storage[1] >= 0.0
 M3_storage[2] >= 0.0
 M3_storage[3] >= 0.0
 M3_storage[4] >= 0.0

But wow, when I add another “feature” and would like to use the already existing model m I get an error:

function add_min_level!(m::Model,Min_m3::Int64,time_steps::OrdinalRange{Int64,Int64})
    @constraint(m, storageMin[t in time_steps], M3_storage[t]>=Min_m3)
    return m

Output: UndefVarError: M3_storage not defined

M3_storage[t] can be printet with print(m).
But is not available in the function add_min_level! even with (m::Model) shared as imput parameter?

Could you help me what the problem is and how to resolve it.
Generally what is the best in JUMP to make a flexible optimization Model where you can add or delete easily model componentes

Best Greetings Gerhard

Just add M3_storage = m[:M3_storage] as a first line of your new function

Great thank you for your solution!

Generally what is in your view the best way in JUMP to make an optimization model flexible? Where you can easily add or delete or change: model componentes / the included energy system sectors /
features / equations?

I was thinking to use multiple functions(to optionally add differen model components and constraints) and also the .add_to_expression! — command.
For example for the electricity balance: sum of production units = sum demands
If the model components(different optional generators and demand sections) are added optionally piece by piece, it seems to be necessary to use add_to_expression to collect the different new inputs into the balance and at the end then create the constraint based on what is in this expression.