A battlement in defensive architecture, such as that of city walls or castles, comprises a parapet (i.e., a defensive low wall between chest-height and head-height), in which gaps or indentations, which are often rectangular, occur at intervals to allow for the launch of arrows or other projectiles from within the defences. These gaps are termed " crenels" (also known as carnels, or embrasures), and a wall or building with them is called crenellated; alternative (older) terms are castellated and embattled. The act of adding crenels to a previously unbroken parapet is termed crenellation.
The function of battlements in war is to protect the defenders by giving them something to hide behind, from which they can pop out to launch their own missiles. A defensive building might be designed and built with battlements, or a manor house might be fortified by adding battlements, where no parapet previously existed, or cutting crenellations into its existing parapet wall. A distinctive feature of late medieval English church architecture is to crenellate the tops of church towers, and often the tops of lower walls. These are essentially decorative rather than functional, as are many examples on secular buildings.
The solid widths between the crenels are called merlons. Battlements on walls have protected walkways ( chemin de ronde) behind them. On tower or building tops, the (often flat) roof is used as the protected fighting platform.
The term originated in about the 14th century from the Old French word batailler, "to fortify with batailles" (fixed or movable turrets of defence). The word crenel derives from the ancient French cren (modern French cran), Latin crena, meaning a notch, mortice or other gap cut out often to receive another element or fixing; see also crenation. The modern French word for crenel is créneau, also used to describe a gap of any kind, for example a parking space at the side of the road between two cars, interval between groups of marching troops or a timeslot in a broadcast. 
In medieval England and Wales a licence to crenellate granted the holder permission to fortify their property. Such licences were granted by the king, and by the rulers of the counties palatine within their jurisdictions, e.g. by the Bishops of Durham and the Earls of Chester and after 1351 by the Dukes of Lancaster. The castles in England vastly outnumbered the licences to crenellate.  Royal pardons were obtainable on the payment of an arbitrarily-determined fine by a person who had fortified without licence. The surviving records of such licences, generally issued by letters patent, provide valuable evidence for the dating of ancient buildings. A list of licences issued by the English Crown between the 12th and the 16th centuries was compiled by Turner & Parker and expanded and corrected by Philip Davis and published in The Castle Studies Group Journal. 
There has been academic debate over the purpose of licensing. The view of military-focused historians is that licensing restricted the number of fortifications that could be used against a royal army. The modern view, proposed notably by Charles Coulson, is that battlements became an architectural status-symbol much sought after by the socially ambitious, in Coulson's words: "Licences to crenellate were mainly symbolic representations of lordly status: castellation was the architectural expression of noble rank".   They indicated to the observer that the grantee had obtained "royal recognition, acknowledgment and compliment".   They could, however, provide a basic deterrent against wandering bands of thieves, and it is suggested that the function of battlements was comparable to the modern practice of householders fitting highly visible CC TV and burglar alarms, often merely dummies. The crown usually did not charge for the granting of such licences, but occasionally charged a fee of about half a mark. 
Battlements may be stepped out to overhang the wall below, and may have openings at their bases between the supporting corbels, through which stones or burning objects could be dropped onto attackers or besiegers; these are known as machicolations.
Battlements have been used for thousands of years; the earliest known example is in the fortress at Buhen in Egypt. Battlements were used in the walls surrounding Assyrian towns, as shown on bas reliefs from Nimrud and elsewhere. Traces of them remain at Mycenae in Greece, and some ancient Greek vases suggest the existence of battlements. The Great Wall of China has battlements.
In the European battlements of the Middle Ages the crenel comprised one-third of the width of the merlon: the latter, in addition, could be provided with arrow-loops of various shapes (from simply round to cruciform), depending on the weapon being utilized. Late merlons permitted fire from the first firearms. From the 13th century, the merlons could be connected with wooden shutters ( mantlets) that provided added protection when closed. The shutters were designed to be opened to allow shooters to fire against the attackers, and closed during reloading.
The Romans used low wooden pinnacles for their first aggeres ( terrepleins). In the battlements of Pompeii, additional protection derived from small internal buttresses or spur walls, against which the defender might stand so as to gain complete protection on one side.
Loop-holes were frequent in Italian battlements, where the merlon has much greater height and a distinctive cap. Italian military architects used the so-called Ghibelline or swallowtail battlement, with V-shaped notches in the tops of the merlon, giving a horn-like effect. This would allow the defender to be protected whilst shooting standing fully upright. The normal rectangular merlons were later nicknamed Guelph.
Many South Asian battlements are made up of parapets with peculiarly shaped merlons and complicated systems of loopholes, which differ substantially from rest of the world.  Typical Indian merlons were semicircular and pointed at the top, although they could sometimes be fake: the parapet may be solid and the merlons shown in relief on the outside, as is the case in Chittorgarh. Loopholes could be made both in the merlons themselves, and under the crenels. They could either look forward (to command distant approaches) or downward (to command the foot of the wall). Sometimes a merion was pierced with two or three loopholes, but typically, only one loophole was divided into two or three slits by horizontal or vertical partitions. The shape of loopholes, as well as the shape of merlons, need not have been the same everywhere in the castle, as shown by Kumbhalgarh. 
In Muslim and African fortifications, the merlons often were rounded. The battlements of the Arabs had a more decorative and varied character, and were continued from the 13th century onwards not so much for defensive purposes as for a crowning feature to the walls. They serve a function similar to the cresting found in the Spanish Renaissance architecture.
"Irish" crenellations are a distinctive form that appeared in Ireland between the 14th and 17th centuries. These were battlements of a "stepped" form, with each merlon shaped like an inverted 'T'.     
European architects persistently used battlements as a purely decorative feature throughout the Decorated and Perpendicular periods of Gothic architecture. They not only occur on parapets but on the transoms of windows and on the tie-beams of roofs and on screens, and even on Tudor chimney-pots. A further decorative treatment appears in the elaborate paneling of the merlons and that portion of the parapet walls rising above the cornice, by the introduction of quatrefoils and other conventional forms filled with foliage and shield.