I've been looking for a detailed explanation for how the transponder works and the importance of a squawk code.
The squawk code is assigned by Air Traffic Control to the aircraft and is used for radar identification purposes. It is a 4 digit octal number (each digit has a value in the range [0-7]).
The flight crew enters the assigned squawk code into the transponder control panel and the radar extracts the code using a Mode-A interrogation (or UF5 / UF21 interrogation in case of a Mode S radar/ transponder).
The extracted squawk code can then be used to correlate the radar reply with an aircraft's flight plan. A number of squawk codes are reserved for generic purposes and emergencies. These codes are used in emergency, or when no flight plan correlation is needed. Some generic codes are:
1000 - Conspicuity code signalling the Flight Data Processing system to use Mode-S Aircraft ID for flight plan correlation. (Primarily in Europe)
1200 - Visual Flight Rules standard squawk code (USA & Canada)
2000 - Used when entering a Secondary Surveillance Area and no code has yet been assigned.
7000 - Visual Flight Rules standard squawk code (ICAO, USA & Canada use 1200 instead)
7500 - Unlawful interference / hijack (ICAO, worldwide)
7600 - Radio communication lost (ICAO, worldwide)
7700 - General emergency (ICAO, worldwide)
It allows Secondary radar to identify each aircraft. The response ping from the aircraft contains the 12 bit squawk code.
Each plane is assigned a different squawk code as they enter an airspace and the software compares that to the flight plans filed and then can display the relevant information on the display.
If you fly VFR without a flight plan you will squawk the general VFR code until asked to change it when entering controlled airspace. When leaving tghe controlled airspace you will be asked to "squawk VFR" again.
They can also be used to declare emergency by squawking 7700, 7600 or 7500 (for general emergency, radio loss and hijack resp.)