Sleep does not have A function, rather its functions can be regarded from several perspectives: for example (not exclusively) there are biological, medical, psychological and spiritual functions.
And form each of these perspectives there is a positive and negative function: the positive function describes what sleep does, and the negative function what bad things happen when this function is not performed (which may not be obviously-related to the positive function).
And within these functions there will probably be different functions for Deep (Non-REM) and Dreaming (REM) types of Sleep.
Biological function of Deep Sleep and Dreaming Sleep
The mind (which includes the Awake state, and the two types of Deep and Dreaming sleep) is a complex system, and can be analysed as such.
1. Deep Sleep (NREM)
Deep sleep is a cyclical process (as reflected in the regular slow wave patterns of EEG), and is restorative and reparative in its function.
(In waking life cyclical processes are typically associated with the development of habits and skills - with improvement in specific types of functionality : for example the repeating 'drills' used in learning a foreign language, and the repetitious 'practice' used in learning a musical instrument or sporting technique. It is noticable how new skills that are practiced using repetitious drills often become learned only after a sufficient period of restorative sleep.)
More exactly, the cycles of Deep sleep serve to associate and integrate the complex processes of sleeping and waking - because sleep and waking are both complex entities which include variations in the degree and coordination of processes such as awareness, muscle tone, will, memory.
Inadequate Deep sleep therefore leads to the dissociation of sleep processes (and dis-integration of Waking phase life), which may be seen as symptoms of intrusive sleep into waking, with poor concentration, thought disorder - interrupted streams of thought for example with hallucinations - and to delirium (clouded consciousness, disorientation) and psychotic symptoms as well as changes in mood such as depression or elation (mania).
These problems are essentially problems of harmonization, coordination, synchronisation, integration and correct-association of dissociable mental processes: deep sleep activates, suppresses and links-between these processes appropriately; and lacking adequate Deep sleep, the proper associations break down.
In a (restricted) computing analogy, the cyclical aspects of Deep sleep are somewhat like the multiple 'Restarts' necessary when patching and repairing the software problems of a dysfunctional computer.
In one word, Deep sleep is about healing.
And this further implies that Waking life generates pathologies which require healing.
2. Dreaming Sleep (REM)
To continue the computing analogy, Dreaming sleep is a way of testing the integrity of the complex system of the mind.
In a word - dreaming sleep is a simulation.
In Dreaming sleep, the various internal processes of the brain are allowed to run, freely and in an open-ended fashion, to simulate may situations and combinations of situations, to test the coherence and integration between the sub-systems.
So if deep sleep is about healing and associating, then Dreaming sleep is about testing the effectiveness and integrity of this healing.
An analogy from computing might be that Dreaming sleep is similar to the period of testing following an attempted repair of a glitch or bug, or following a software patch or update.
In terms of medicine, if Deep sleep is about healing, perhaps an operation to repair a broken leg - then Dreaming sleep is somewhat like the period of physiotherapy and rehabilitation which tests the integrity of that repair and various stresses.
If Dreaming sleep is about testing, then this may provide a hint of why dreams can be diagnostic - mostly dreams tell us, when dreams tell us anything, that something is wrong.
But it can be hard to understand exactly what is wrong, what that 'something' actually is, since dreams are in general forgotten, and the process of testing does not depend upon conscious awareness or memory of dream content).
Therefore, the problems detected during dreaming sleep are mostly subsequently dealt with during following periods of Deep sleep - either in a phase of deep sleep immediately following dreaming sleep, or else the next or following night of sleep.
Lack of Dreaming sleep means lack of testing, poor quality control of mental repairs, and the consequences are not as devastating as lack of deep sleep.
Loss of Dreaming sleep (for example when REM sleep is suppressed by a drug) (and what follows is somewhat speculative...) seems to produce subtle symptoms are shallowness and hardening of personality and coarsening of character, desensitization or inappropriate responsivity to the environment, and a non-psychotic level of dis-integration with phenomena such as mood swings, anxiety, impaired concentration, impaired creativity.
After some days, Dreaming sleep (REM patterned EEGs) break through to 'interrupt' both Deep sleep and the awake state - as if the brain had moved form its daily cycle of long phases of compartmentalized phases of Awake, Deep and Dreaming sleep - or Damage/ Repair/ Testing functions - into short, less-compartmentalized micro-cycles which mix together these Awake, Deep and Dreaming phases and functions to produce a more-continuous and less-efficient mixed state of variable amplitude and balance between damage, repair and testing.
In other words, suppression of Dreaming (REM) sleep pushes towards shallow, unsatisfying, unrestorative sleep characterized by multiple awakening and continuous dreaming; and drowsy, inattentive wakefulness prone to narcoleptic phenomena (with sudden onsets of Dreaming sleep).
So, what is the function of Waking life?
Well, at this level of analysis, the biological function of Waking life is proximately to interact with the environment - to perceive the salient features of the environment and behave appropriately; and at a distal level the purpose of this interaction is survival and reproduction.
However, this Waking life interaction is conceptualized (using the systems theory approach) as being disintegrative and disruptive. Thus interaction with the environment breaks up the harmonious interaction of mental processes, and creates bugs and glitches that accumulate and impair function unless or until these problems are repaired by Deep sleep.