Great, thanks Rohit.
I just came by ToExpression. One needs to build a string "funcX[x_ ,y_ ] := <value of body>
" and execute this string with ToExpression.
My program looks as follows. The only thing that does not yet work is to Remove or Clear funcX before the new definition.
Remove[funcX];
funcX[z_] := Sin[z];(* Just for fun *)
(*Echo[Information[funcX],"any old funcX: "];*)
Block[{funcName, args, body, func, funcDef, x, y},
(* Given: *)
funcName = "funcX";
args = {x, y};
body = (x + y)^2;
(* func=funcX and funkDef = "funcX[x_,y_]" *)
func = Symbol[funcName];
funcDef =
StringRiffle[(ToString[#] <> "_") & /@ args, {funcName <> "[", ",",
"]"}];
Echo[Grid[{
{"funcName:", funcName // InputForm}, {"args:", args}, {"body:",
body}, {}, {"func:", func}, {"?func:",
Information[func]}, {"funcDef:", funcDef // InputForm}
}, Frame -> True, Alignment -> {{Right, Left}}],
"values so far: "];
(* Define function *)
Remove[funcName];(* This doesn't work. Strange *)
Clear[funcName];
ToExpression[funcDef <> ":=" <> ToString[InputForm[body]]];
Echo[Information[func], "?func after Clear and definition: "];
];
Echo[{Information[funcX], funcX[a, b]}, "the new funcX: "];