Why do aircraft allow sliding / opening windows in cockpits? What purpose does it serve, and why the same can't be done for passenger windows as well?
The opening cockpit windows do serve some purpose, like providing an additional means of evacuation during an emergency.* There have been instances where the crew had used the cockpit windows to escape during hijackings.
There are other purposes like assisting in crew inspection, signaling ground crew, additional ventilation, smoke egress etc (It can also be used for throwing things out, though it is not generally recommended). One main reason (atleast in the past) appears to be to improve the pilot visibility in case the windshield becomes opaque, for example.
The windows are not found in all aircraft (for eg. 787 doesn't have it) and are not very easy to open (note that it opens inwards). From USAToday Ask the Captain:
First there is a locking mechanism, then either a crank which moves the entire window assembly including the frame, or a lever with which to move the window assembly. When the window is closed and locked, it is sealed shut. When open, it does not separate from the frame as the window in your car does.
Actually, FAR 25 Section 773- Pilot Compartment View requires cockpit windows to be openable (or some other means to have clear windshield during precipitation) :
(b) Precipitation conditions. For precipitation conditions, the following apply:
(1) The airplane must have a means to maintain a clear portion of the windshield, during precipitation conditions, sufficient for both pilots to have a sufficiently extensive view along the flight path in normal flight attitudes of the airplane.
(3) The first pilot must have a window that—
(i) Is openable under the conditions prescribed in paragraph (b)(1) of this section when the cabin is not pressurized;
(4) The openable window specified in paragraph (b)(3) of this section need not be provided if it is shown that an area of the transparent surface will remain clear sufficient for at least one pilot to land the airplane safely in the event of—
(i) Any system failure or combination of failures which is not extremely improbable, in accordance with § 25.1309, under the precipitation conditions specified in paragraph (b)(1) of this section.
(ii) An encounter with severe hail, birds, or insects.
In a pressurized cabin, providing windows is not a great idea. (In addition to the increased weight), if anyone manages to open the window, it would be a recipe for disaster (depressurization -> oxygen masks -> land).
*Many aircraft have escape ropes mounted in a compartment above the window on each side of the cockpit as in this A320: Photo source
And a video of it being used.
In the Pilatus PC-12, the pilot's side has a "DV window", a "direct view windows," that is hinged at the back and opens so the pilot has an unobstructed view out forward of the aircraft.
This window is specifically designed to allow the pilot to land an aircraft when the windshield is iced over, and the de-icing equipment is INOP.
Other answers provide good broad picture information. Let me add a specific example.
The windows in my work aircraft open, and we have specifically modified them to open fully out of view and remain open. Windows that open allow me to better perform my job. Note that my aircraft is non-pressurized.
One of the things we often do is drop messages to people on the ground. The open window provides a means of jettisoning that message package.
Aerial photography and observation of the ground is in integral part of what we do. Being able to open the window to provide an unrestricted view can give better photo or viewing environment. Conditions such as sun glare render this an important feature.
Finally, being able to open the window on a hot day can be a welcome addition to the ventilation system.
and why the same can't be done for passenger windows as well?
Anybody who opens even a bit a potential airliner sliding window near his seat creates in the entire plane the conditions existent on the top of Mount Everest, even worse. Nobody wants to stay in such an environment. In conclusion passenger sliding windows serve no purpose.