Hi Akash,
Good question. In addition to the above, as you start to work with code (and input) that isn't just your own, the concept of function "pre-conditions" and "post-conditions" becomes a bit more apparent and practical. When it's all just your own code, that can easily be changed, the concepts seem a bit foreign.
Consider a case where you're going to use a library called "Temperature" written by Againtry. It might supply a function in its interface that converts Fahrenheit to Celsius, like so:
|
float Fahrenheit_To_Celsius(float f);
|
You likely have very little idea about how that function is implemented (and you're not responsible for any verification that goes on within it), but Againtry has made a type of agreement with you: "If you give me a float that represents a valid Fahrenheit, I will give you back the same temperature in Celsius".
The functions "Pre-condition" is: Fahrenheit_To_Celsius expects a valid float that represents a Fahrenheit.
The functions "Post-condition" is: Fahrenheit_To_Celsius will return a valid float that represents the same temperature in Celcius, if it's Pre-condition is satisfied
With all of that said: Say your fahrenheit input is coming from some end-user -- then it's
your job to make sure that you verify the user's input before calling Fahrenheit_To_Celsius so that it receives the correct input to satisfy its pre-condition and it's
Againtrys job to make sure that he does the conversion properly and returns a correct value.
It's a trite example, and maybe not precisely all of the things in the above Wiki, but realizing that not all code is going to be your own is sometimes a concept that eludes people who are beginning.
EDIT: It's also worth noting that, at some point, the creator of code has to assume some sort of sanity on the caller's part, given a clear enough interface. If you try to solve every single verification case, your code quickly becomes hard to decipher. So, in this case Againtry will probably not do many checks in his function besides maybe a bounds check, he's going to write the function assuming the caller has some type of sanity and is calling it with the correct pre-condition met.
Hope that helped.