Offline Training of Language Model Agents with Functions as Learnable Weights