How to write data from user-defined function