In this example , the function named gradient forms a closure together with the variables f and dx . This closure is then returned by the outer function named derivative . At runtime , when the outer function executes , a closure is formed , consisting of the inner functionâ s code and references to any variables of the outer function required by the closure ; such variables are called the upvalues of the closure .