Python Programming Function Parameters Tutorial with Stone River eLearning
In the previous blog post on this tutorial series called Python for Beginners, we discussed about functions and their implementation in Python. In this blog post, we are going to move forward with our discussion on functions and include the different variations that one can use while implementing them in Python.
Parameterized Functions can be executed in Python just like they are executed in any other programming language. The arguments are passed to the function as parameters and the same are then used in the operations performed by the function. The arguments that are passed as parameters, act as variables inside the function’s domain and these variables are then assigned values on which calculations or operations are done inside the function to deliver desired results. In some cases, the arguments passed to the parameters are values themselves and the same can be used for the operations inside the functions. The number of parameters that can be passed to a Python function as arguments is not limited to a number.
Note:
- Even though there is no restriction on the number of arguments passed to a Python function, it is important to keep in mind that Python can process only as many number of parameters as the arguments passed to it. In other words, if you pass three arguments and then initialize values for more than these three arguments in the function, then Python will not run and show an error.
- The values to be given to a parameter that is passed to a function muse be carefully assigned in case of functions with multiple parameters. Any discrepancy in this part will not be detected and the Python program will run but show an output on the console that is not desired.
Eg:
def addition (num1, num2) : answer = num1+num2 return answer x = addition (5,6) print(x)
In the above example, it is evident that the values passed to the function call are same in number as the number of parameters mentioned in the function definition. The arguments are passed as values which are then used to perform calculations inside the function.
Since making sure that the number of arguments in function call are the same as number of parameters in function definition can be both tricky and unnecessary in programs that involve a lot of arguments, Python gives the programmers a brilliant way of avoiding the pain of writing every function parameter even if they do not intend to use each one of them. Assigning default values to the function allows the programmer the privilege to later change to values of only the parameter that he wants to modify without editing the other values.
Eg:
def website ( font=’TNR’, color =’white’ , size=’11’) print (‘font:’, font) print(‘size:’,size_new) print(‘color:’,color_new) website(color_new = ‘grey’)
If you look at the above example closely, you will note that the arguments passed to the function by the name ‘website’, are then set as default by saving them to a variable. This new variable can then later be used anytime to assign a new value of edit the current value in the function parameter. It should be noted that even though this saves a lot of time and effort, such an operation is not feasible in case your program has numerous function calls and definitions as setting defaults for each one of them will affect the performance and speed of the program.
GET YOUR FREE PYTHON EBOOK!