Professional Documents
Culture Documents
Discussion Forum Cs 1101 Unit 4
Discussion Forum Cs 1101 Unit 4
correcting them. Functions exist not only in Python, but in almost every program language,
because it is necessary and useful to arrange and simplify the code, in such case it is important
to discuss about when the function fail in some way and are three possibilities to cause a
function failure.
1. There is something wrong with the arguments the function is getting, a precondition
is violated; it is what you need at the start to the function do and generate the desire
outcome.
example:
def somefunction(x,y):
z = x+y
return z
somefunction(d,33)
Output:
Exception has occurred: NameError
2. There is something wrong with the function; a postcondition is violated. It happens after the function
complete, when the syntax is correct but when you run the code produce a wrong result.
example:
def somefunction(x,y):
print (x*y)
somefunction(3,2)
Output:
6
It happens because the Precondition would be that x and y must be integers and the post
condition would be that the function must produce the sum of x and y.
3. There is something wrong with the return value or the way it is being used.
Example
def somefunction(x,y):
z = (x+y)
return str(z)
any = somefunction(5,9) * 2
print (any)
Output:
1414
what happens is the variable z is returned as a string producing 14 and after 14 doing this 2
times, instead of multiplier 14 to 2 which would be 28.