How to create a function dynamicaly ?

I think you'll need write your own library. No, you can't dynamically write function but what you want to do is possible.

You'd want a class where to can pass in timed events and info of other devices you want to control, maybe an array of pins or object of devices.