Can you really allocate virtual memory for any byte array that contains a function, write the array within the memory after which in some way execute the function in virtual memory?

You've to be certain the bytes you will execute truly are a legitimate sequence of CPU instructions, or else the "function" would certainly crash, for example leading to exceptions "illegal instruction", "access breach" etc.

Next factor to complete would be to make certain the bytes under consideration are situated in memory which has a execution privilege. VirtualProtect with PAGE_EXECUTE enable you to get that.

Then you're to really pass control for your function. This is often set up code, e.g. push <address>; ret; or C++ style call of static function with address in variable.