Any defect, shortcoming or error in software (built to perform a specific function or set of functions) is known as a Bug. A bug is usually an unexpected event encountered by a tester (or sometimes a programmer himself) while testing a unit, module or a complete product. This unexpected event could be due to a coding error, a code defect, code fault, flaw in coding, imperfect coding, erroneous coding, or improper coding.
The purpose of a code is to perform a pre-defined or pre-conceived function or set of functions. Once the desired function is not performed or is performed with some errors, the code is known to contain bug(s) which need to be fixed. The ultimate goal of a code is to perform as it is intended to perform under all circumstances.
The software built is meant to perform perfectly under all scenarios/ customer requirements/ business rules built in the software to satisfy all functional and business requirements of the customer.