Best Laptop
Parameterized Functions in Python

Parameterized Functions in Python

Hi Friends, in previous session we saw an introduction to Python Function. In this session, we will learn about Parameterized Functions in Python. Parameterization is a way of passing data to the function so that the function body use that data and complete the task. Parameters are just simple variables that are defined in function and exist only with the function body.

Important Points to remember for Parameterized Functions in Python:

• Parameters are defined with the parenthesis present in the def statement
def fun(num): — function definition
• Parameter life is only with in function body. Outside function, if we try to use the parameter, we will see error
• We can pass value to the function parameter while making a call to the function. In other words, we pass argument while calling a function and the value of the argument is assigned to the parameter present in function definition
fun(5) – function call

Examples of Parameterized Functions in Python:

1. Using parameter in function

def integer(num):
     print("Number is:",num)
integer(10)

2. Make sure that if the function definition contains parameter, then while calling the function, we have to provide the argument otherwise it will fail

def integer(num):
     print("Number is:",num)
integer()
Error: TypeError: integer() missing 1 required positional argument: 'num'

3. Also remember that if there is no parameter in function definition but while calling the function, we provide argument, then also we will see error.

def integer():
     print("Number is:",num)
integer(10)
Error: TypeError: integer() takes 0 positional arguments but 1 was given

4. Parameter exist only with in the function. Hence, we can use same name for the parameter present in function definition and any other variable outside function. In below example, we have used a parameter ‘num’. Outside the function also, we have defined a variable with name ‘num’. Parameter ‘num’ will take value provided via argument on function call. Variable ‘num’ takes the value provided outside the function body in the code.

def integer(num):
     print("Number is:",num)
integer(10)
num=5
print(num)
integer(20)

This shows that parameter num and variable num and different from each other. This is also called as shadowing where parameter inside the function shadows any other variable of the same present outside the function.

5. Function can have more than one parameter

def integer(num1, num2):
     print("First number is:",num1)
     print("Second number is:",num2)
integer(10,20)

Here first argument value will be assigned to first parameter, second argument to second parameter and so on. Arguments passed in this way is called as Positional Arguments Passing and Parameters assigned in this way is called as Positional Parameter Passing.

6. We have to provide same number of arguments in function call as there are parameters in function definition otherwise it will give error

def integer(num1, num2):
     print("First number is:",num1)
     print("Second number is:",num2)
integer(10)
Error: TypeError: integer() missing 1 required positional argument: 'num2'

7. We can also provide different data type parameter in function. Here first argument is a string and second is a float value.

def integer(language, version):
     print("Language is:",language)
     print("Version is:",version)
integer("Python",3.0)

8. We can use parameters in the function in any sequence like below:

def integer(language, version):
     print("Version is:",version)
     print("Language is:",language)
integer("Python",3.0)

9. Keyword Argument Passing – here which argument-parameter assignment is not decided by position of argument but by the name of the argument. In this case, while passing the argument, we have to specify the name of the parameter. Hence the position of the argument does not matter

def integer(country, currency):
     print("Country:",country)
     print("Currency:",currency)
integer(currency="INR",country="India")

10. In Keyword Argument passing, nonexistent parameter name should not be used

def integer(country, currency):
     print("Country:",country)
     print("Currency:",currency)
integer(currency="INR",name="India")
Error: TypeError: introduction() got an unexpected keyword argument 'name'

11. Using both Keyword Argument and Positional Argument while passing argument. Here we just have to remember to put Positional Arguments before Keyword Arguments

def adding(a,b,c):
     print("sum of three number is:",a+b+c)
adding(10,c=30,b=20)

Here argument=10 is passed to parameter using positional way of passing Argument for c and b are passed using keyword passing hence sequence does not matter. Remember that first we have used positional way of passing then keyword way

12. One argument should not be provided more than one value

def summation(a,b,c):
     print("sum of three number is:",a+b+c)
summation(10,a=30,b=20)
Error: TypeError: adding() got multiple values for argument 'a'

13. Keyword passing should not precede positional passing else we will see error

def summation(a,b,c):
     print("sum of three number is:",a+b+c)
summation(a=10,c=30,20)

14. In below example we have used position passing for two argument and for third argument, we have used keyword passing.

def summation(a,b,c):
     print("sum of three number is:",a+b+c)
summation(10,20,c=30)

15. Assigning default values to some parameters – in case we want to use same value for some parameter, then instead of passing each time via argument, we can provide a default value in function definition itself. In below example, we have used a default value for version

def name(language,version=3.0):
     print("Language is: ",language,".Version is:",version)
name("Python")

Here since version is already defaulted, the argument “Python” will be assigned to parameter ‘language’

16. We can still use a different value for default parameter by passing an argument explicitly

def name(language,version=3.0):
     print("Language is: ",language,".Version is:",version)
name("Python",2.0)

17. Using default value for all the parameters. In below example, we have used default value for all parameters hence while calling we have not provided any argument

def name(language="Java",version=1.8):
     print("Language is: ",language,".Version is:",version)
name()

18. Using argument for default parameters

def name(language="Java",version=1.8):
     print("Language is: ",language,".Version is:",version)
name(version=3.1,language="Python")

19. Remember that in function definition, non-default argument should not come after a default argument

def multiply(a=1,b=2,c):
     print("Answer is:",abc)
multiple(c=3)

Error: SyntaxError: non-default argument follows default argument

Note: For all the concepts mentioned in all these points, we can create any number of examples. So please spend some on this and create functions in different ways and test.

Previous Session: https://itkaksha.com/functions-in-python-introduction/

One thought on “Parameterized Functions in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Python 3.11.1 Features