Question: why dml not allowed in function?
Answer: A function is supposed to compute and return a result, not change the state of the database. If you want to do DML in a function (i.e. if you want to insert rows into tables), you cannot call that function in a SELECT statement since a SELECT statement cannot change the state of the database.