When you debug a code, it means you locate and correct the error(s) in the code. The three possibilities of a function not working properly are:
Invalid arguments
This arises when the number of parameters does not match with the number of arguments passed to the function.
This is also referred to as violation of precondition
An example in python is as follows:
def add_two_num(a,b):
return a + b
add_two_num(5)
The above function receives 1 parameter, instead of 2
Incorrect function operation
This arises when the function does not perform the required operation.
This is also referred to as violation of post-condition
An example in python is as follows:
def square_num(a):
return a * 2
The above function returns twice a number, instead of its square.
Incorrect return value or return type
This arises when the function returns a different value or data type, different from what it is expected to return
An example in python is as follows:
def square_num(a):
sq = a**2
return sq
The above function is expected to return the square of a number, but it returns the number itself.
Comments
Leave a comment