While writing a python function and using it in your program, you get lot of errors such typing wrong name of the function, changing the order of definition and function calls, wrong parameters, less parameters(parameter mismatch) and scope errors.
Wrong Function Definition and Function Call Order
In python programming one of the most common error is calling the function even before you define your function. The function definition runs first and creates the function in python programming before you can call it. If you call the function earlier than python will throw an error. Consider the following example.
print(add(23,67)) def add(n1, n2): return n1 + n2
The program above will try to execute print statement –
print(23, 67) and it will not find the function
add(). This will result in following error.
= RESTART: C:/PythonExercises/Wrong_definition_function_call_order.py Traceback (most recent call last): File "C:/PROGRAMMING/PythonExercises/Wrong_definition_function_call_order.py", line 1, in <module> print(add(23,67)) NameError: name 'add' is not defined >>>
Parameter Mismatch Error
A lot of python functions are written with multiple parameters.However, when you call the function with either wrong parameter type or less number of parameters, python will throw an error in each case.
def expression_1(num1, num2, num3): return (num1 + num2)/num3 # But the function call has wrong type expression_1("345", 55, 66)
Consider the parameter type mismatch error example. In the program below, the function is expecting a number, but receives a string number. Therefore, throws an error.
=== RESTART: C:/PythonExercises/Functions/type_mismatch_error.py === Traceback (most recent call last): File "C:/PythonExercises/Functions/type_mismatch_error.py", line 5, in <module> expression_1("345", 55, 66) File "C:/PythonExercises/Functions/type_mismatch_error.py", line 2, in expression_1 return (num1 + num2)/num3 TypeError: can only concatenate str (not "int") to str
From the above example, it clearly evident that there is a type mismatch between the function call and function definition.
The second type of error happens when the function call does not have the correct number of arguments, according to parameters.
# function definition def expression_2(num1, num2, num3): return num1 * num2 + num3 # function call with only two parameters, missing third expression_2(10,44)
The above program missing the third argument according to third parameter in the function definition. Note that the argument is the value replaced with the parameter. Therefore, number of arguments must match number of parameters.
Traceback (most recent call last): File "C:/PROGRAMMING/PythonExercises/Functions/number_of_arguments_mismatch_error.py", line 5, in <module> expression_2(10,44) TypeError: expression_2() missing 1 required positional argument: 'num3' >>>
The error clearly says that the
num3 is missing.
In python, a local variable that is created inside of the function cannot be called outside of the function. This is possible in the case of a conditional block such as if, if-then-else, etc. But not in python functions.This is called a scope error. Consider the following example. This example tries to find the maximum of two numbers.
def find_max(n1, n2): num1 = n1 num2 = n2 if num1 > num2: result = num1 elif num2 > num1: result = num2 else: result = num1 find_max(34,77) print(result)
The program tries to print the variable called result created inside of the function. But it does not work and gives following error.
Traceback (most recent call last): File "C:/PROGRAMMING/PythonExercises/Functions/Scope_Error.py", line 13, in <module> print(result) NameError: name 'result' is not defined
To fix the problem, you have to assign the function itself to a variable and then print it. Consider the modified program below.
def find_max(n1, n2): num1 = n1 num2 = n2 if num1 > num2: result = num1 elif num2 > num1: result = num2 else: result = num1 return result maxNum = find_max(34,77) print(maxNum)
The output of the program is printed successfully.
===== RESTART: C:/PythonExercises/Scope_Error.py ======= 77 >>>
In the next article, we will talk about function that returns None type.