Hello!
The most honest answer is that we do not yet know the function or functions ofdreaming. This ignorance should not be surprising because despite many theories we still do not fully understand the purpose of sleep, nor do we know the functions of REM (rapid eye movement) sleep, which is when most dreaming occurs.
Have a nice day