Does artificial intelligence write its own code?
Does artificial intelligence write its own code and then execute it? If so, does it create separate functions for each purpose?


Artificial intelligence comprises more than one technique. Here you seem to be asking about machine learning, a subfield of AI which itself contains many subfields.

Self-modifying code is one of the (many) techniques used in some applications of artificial intelligence.  So, in that sense, the answer to your question is "yes".

In Neural Networks (NNs), for example, the NN isn't writing any code, it is just running through an optimization algorithm (and back-propagation) that incrementally changes some weights (or coefficients) such that when you enter a certain input, you get an output that's close to the desired output.  

You also have genetic algorithms (GA) to evolve the weights of a neural network, as opposed to using back-propagation. GAs are closer to the idea of "an AI coding itself" (although not exactly).