Here is one example of function which will add a number. It is missing some checks to be a "good" function but does provide the necessary idea that values which might change (column name and number to add in this case) must be evaluated (see Insert one expression into another... I mentioned earlier)
Names Default To Here(1);
dt = open("$SAMPLE_DATA/Big Class.jmp");
cols = {"height", "weight"};
add_num_to_col = Function({dt, colname, num_to_add}, {Default Local},
new_col = Eval(EvalExpr(
dt << New Column(colname || " add " || char(num_to_add), Numeric, Continuous, Formula(
Expr(NameExpr(AsColumn(dt, colname))) + Expr(num_to_add)
));
));
return(new_col);
);
col1 = add_num_to_col(dt, "height", 10);
col2 = add_num_to_col(dt, "height", 20);
col3 = add_num_to_col(dt, "weight", 5);
Write();
Sometimes using Eval(Substitute()) is cleaner and easier to understand solution and it could look something like this
add_num_to_col = Function({dt, colname, num_to_add}, {Default Local},
new_col = Eval(Substitute(
Expr(dt << New Column(colname || " add " || char(num_to_add), Numeric, Continuous, Formula(
_colname_ + _num_to_add_
))),
Expr(_colname_), Name Expr(AsColumn(dt, colname)),
Expr(_num_to_add_), num_to_add
));
return(new_col);
);
-Jarmo