Parameter Passing methods in Python | 3 Important Method or Techniques | CBSE Class 12
In this tutorial, we shell be learning about different method of passing argument values according to defined parameters in a function header and learn how can we use different Parameter passing method while execution in a python program.
Python mainly provides the following method to pass parameter in a python program.
- Positional argument /required argument
- Default arguments
- keyword / named parameter
So far, we have learnt that a function call must provide all the values as required by function definition.
For instance, if a function header has three parameters then the function call should also pass exactly three values.
Other than this Python also provide some other ways of sending and matching arguments and parameters through the above given methods of passing parameters like
- Positional argument /required argument
- Default argument
- Keyword / named argument
Let’s discuss these types blow one by one
For more detailed explanation , you can watch these vedeo classes
Positional arguments /required arguments in python
when you create a function call statement for a given function definition then you need to match the number of argument with number of parameters required
for example if a function definition header is like
def calculate (a, b, c ):
- - - - - - - - - -
- - - - - - - - - -
then possible function calls for this are
-
calculate (a,b,c)
calculate (2,b,c)
calculate (2,5,7)
From the above example it is clear that if we pass 3 consecutive values 2,5,7 for a function call, then all these values will be given in the same order or sequence means that
-
- a will get value 2
- b will get value 5
- c will get value 7
Thus, through such function calls you understand that
- the argument must be provided for all parameters (required)
- the value of argument must matched with parameter position (order) wise (position).
This method of parameter passing is called Positional argument OR Required argument OR Mandatory argument
Important Points
- no value can be escaped from function call.
- you cannot change the order you cannot assign value first argument to third or second parameter.
Default arguments in python
Python allows you to assign default values to a function parameter and the default values specified in the function header of the function definition
here we are giving an example for your better understanding. Suppose a function header is defines like this.
def interest (principal, time ,rate= 0.15)
Where rate =0.15 is the default value for parameter rate. If in a function call the value of rate is not provided then Python will use this value of rate while execution.
For example
interest (1200, 3)
in this function call default value for rate will be used i.e rate -0.15 because no value is being passed while executing then the default value is used.
another example
interest (1200, 3, 0.10)
in this function call value for rate will be used i.e rate -0.10 because a value (0.10) is being passed while executing then here the passed value (0.10) will override the default value and 0.10 will be used as rate =0.10
Now the definition :-
A parameter having default value in the function header is called as default parameter.
keyword / named arguments in python
To have complete control and flexibility over the values send to the function as argument for corresponding parameter Python provides one special type of argument that is keyword argument. In keyword argument passing method you can write any argument in any order by providing names to the values being passed as argument when calling this function
for example,
simple function calls with keyword arguments
-
-
interest( princcipal= 1200, time=2, rate=0.15 )
interest(time=2 , princcipal= 1200, rate=0.15 )
interest(time=2, rate=0.15 , princcipal= 1200)
-
Definition :- Keyword argument are the name argument with assigned values being passed in the function call statement.
Difference between parameters and arguments
In Python, the terms “parameters” and “arguments” are often used interchangeably, but they actually have different meanings.
“Parameters” refer to the variables defined in the function signature. They represent the input values that a function expects to receive when it is called. For example:
In this example, x
and y
are the parameters of the add_numbers()
function.
“Arguments,” on the other hand, refer to the values that are passed into a function when it is called. They are the actual values that are used to replace the parameter variables in the function body. For example:
In this example, 3
and 5
are the arguments that are passed into the add_numbers()
function. The value 3
is assigned to the parameter variable x
, and the value 5
is assigned to the parameter variable y
.
It’s important to note that the number of arguments must match the number of parameters in a function, otherwise, a TypeError
will be raised.
Understanding the difference between parameters and arguments is important when writing and calling functions in Python. By using the correct terminology, you can write clearer and more effective code that is easier to read and understand.
How to give a parameter to a function in Python?
In Python, you can give parameters to a function in the function definition.
The syntax for defining a function with parameters is as follows:
def function_name(param1, param2, ...):
# Function body
In this syntax, function_name
is the name of the function, param1
, param2
, and so on, are the parameters that the function takes. You can define as many parameters as needed by separating them with commas.
When you call the function, you can pass arguments to the function using the following syntax:
function_name(arg1, arg2, ...)
In this syntax, function_name
is the name of the function, and arg1
, arg2
, and so on, are the arguments that you want to pass to the function. The arguments should be passed in the order that the parameters are defined in the function.
For example, consider the following function:
def greet(name):
print("Hello, " + name + "!")
To call this function and pass it an argument, you can do the following:
greet("Alice")
In this example, the string "Alice"
is passed as an argument to the greet
function. The function takes this argument and uses it to print out the greeting message.
How to pass a function parameter to another function in Python?
Here’s an example:
def add(a, b):
return a + b
def multiply(a, b):
return a * b
def combine(func, a, b):
return func(a, b)
result = combine(add, 2, 3)
print(result) # Output: 5
result = combine(multiply, 2, 3)
print(result) # Output: 6
In this example, there are three functions defined: add
, multiply
, and combine
. The combine
function takes a function as its first argument, and two values as its second and third arguments. The combine
function then calls the function that was passed to it, with the two values that were also passed to it.
The result
variable is assigned the return value of the combine
function with add
as the function argument, and with the values 2
and 3
. The result
variable is then printed to the console. The same process is then repeated with the multiply
function.
In this way, you can pass a function parameter to another function and use it to perform some computation.
Exam Time
IMPORTANT QUESTIONS
Asked in examinations of Class 12th Computer Science
Answer: Question: What are arguments and parameters in Python?
Parameters are the variables that are defined in the function signature and serve as placeholders for the values that will be passed to the function. Arguments, on the other hand, are the actual values that are passed to the function when it is called.
Question: What are parameter passing techniques in Python?
Answer: Parameter passing techniques are methods used to pass values to functions in Python. These techniques include positional parameter passing, keyword parameter passing, default parameter passing, and variable-length parameter passing.
Question: What is positional parameter passing in Python?
Answer: Positional parameter passing is a technique in Python where arguments are passed to a function based on their position in the function call. The values of the arguments are assigned to the parameters in the function signature in the order that they appear.
For example:
In this example, the values 1 and 2 are passed to the “add” function in that order, and they are assigned to the parameters “a” and “b” based on their position.
Question: What is keyword parameter passing in Python?
Answer: Keyword parameter passing is a technique in Python where arguments are passed to a function using their parameter names. This allows the arguments to be passed in any order, as long as their parameter names are specified.
For example:
In this example, the arguments “John” and “Hello” are passed to the “greet” function using their parameter names “name” and “message”.
Question: What is default parameter passing in Python?
Answer: Default parameter passing is a technique in Python where default values are assigned to parameters in the function signature. If an argument is not passed for a parameter, the default value is used.
For example:
In this example, the default value for the “name” parameter is “World”. Since no argument is passed for “name”, the default value is used.
Question: What is variable-length parameter passing in Python?
Answer: Variable-length parameter passing is a technique in Python where a function can accept a variable number of arguments. This is done using the * operator in the parameter list.
For example:
In this example, the “sum” function takes a variable number of arguments using the * operator in the parameter list. The function adds up all the arguments and returns the result.
Question: What are the 3 types of parameter passing?
Answer: The three types of parameter passing are:
- Pass by value: This is a parameter passing technique where a copy of the actual parameter value is passed to the function. Any changes made to the parameter inside the function do not affect the original value.
- Pass by reference: This is a parameter passing technique where the memory address of the actual parameter is passed to the function. Any changes made to the parameter inside the function affect the original value.
- Pass by object reference: This is a parameter passing technique used in Python, where a reference to an object is passed to the function. Any changes made to the object inside the function affect the original object. However, if the parameter is reassigned to a new object, it will not affect the original object.
It’s important to note that Python does not support pass by value parameter passing. Instead, it uses pass by object reference, which means that any changes made to a mutable object passed to a function can affect the original object.
Question: What are the 4 types of arguments in Python?
Answer: The four types of arguments in Python are:
- Positional arguments: These are the most common type of arguments in Python. They are passed to a function in the order that they appear in the function call. The function maps the values of these arguments to the parameters in the function definition, based on their position.
- Keyword arguments: These are arguments that are passed to a function with their corresponding parameter names. They are used to specify the values of specific parameters in the function definition, regardless of their position.
- Default arguments: These are arguments that have default values specified in the function definition. If a value for the argument is not provided in the function call, the default value is used.
- Variable-length arguments: These are arguments that allow a function to accept an arbitrary number of arguments. There are two types of variable-length arguments in Python:
- *args: This is used to pass a variable number of non-keyword arguments to a function. The arguments are packed into a tuple, which can be accessed inside the function.
- **kwargs: This is used to pass a variable number of keyword arguments to a function. The arguments are packed into a dictionary, which can be accessed inside the function.
Understanding these different types of arguments in Python can help you write more flexible and powerful functions.
Question: How do you pass a parameter to a function?
Answer: To pass a parameter to a function in Python, you can simply include the parameter inside the parentheses of the function call.
Here’s an example:
def greet(name):
greet(“John”)
print("Hello, " + name + "!")
In this example, the greet
function takes a parameter called name
, which is used to print a greeting message. The function is called with the argument "John"
, which is passed to the name
parameter in the function.
When you call a function with parameters, the order of the arguments matters. They should be passed in the same order as the parameters are defined in the function definition.
For example, consider the following function:
def multiply(a, b):
return a * b
To call this function with arguments, you can do the following:
result = multiply(2, 3)
print(result)
In this example, the function is called with the arguments 2
and 3
. These values are passed to the a
and b
parameters in the function, respectively. The function returns the result of multiplying the two arguments, which is stored in the result
variable and printed out to the console.
Question: How can we pass parameters in user-defined functions in python?
Answer: You can pass parameters in user-defined functions in Python by specifying the parameter names inside the parentheses of the function definition. Here’s an example:
def greet(name):
print(f"Hello, {name}!")
greet("John")
In this example, the greet
function takes a parameter called name
, which is used to print a greeting message. When the function is called with the argument "John"
, the value of "John"
is assigned to the name
parameter inside the function.
You can also define functions with multiple parameters by separating the parameter names with commas:
def multiply(x, y):
return x * y
result = multiply(2, 3)
print(result) # Output: 6
In this example, the multiply
function takes two parameters called x
and y
, which are used to perform a multiplication operation. When the function is called with the arguments 2
and 3
, these values are assigned to x
and y
parameters inside the function.
In summary, you can pass parameters to user-defined functions in Python by specifying the parameter names inside the function definition and then passing argument values to those parameters when calling the function.
Question: Are arguments also called as parameters?
Answer: In the context of function definitions, parameters and arguments are closely related concepts but they are not the same thing.
A parameter is a variable declared in the function definition that represents a value passed to the function during its invocation. In other words, a parameter is a placeholder for an argument that will be provided when the function is called.
An argument, on the other hand, is a value that is passed to a function when it is called. Arguments are actual values that are assigned to the corresponding parameters defined in the function.
So, in short, parameters are placeholders in the function definition and arguments are the actual values passed to those placeholders when the function is called.
However, the terms “argument” and “parameter” are often used interchangeably in the programming community, and in many cases, they are not strictly differentiated.
Question: How are arguments passed by value or by reference in python?
Answer: In Python, arguments are passed by assignment, which means that the function parameters are assigned the values of the arguments that are passed to them. Whether the argument is passed by value or by reference depends on the type of the argument.
- Immutable objects (like strings, integers, tuples) are passed by value. This means that when you pass an immutable object to a function, a copy of the object is created, and the function works with this copy rather than the original object. Any changes made to the copy will not affect the original object.
Here’s an example to demonstrate how immutable objects are passed by value:
def add_one(x):
x += 1
print("Inside function:", x)
num = 10
add_one(num)
print("Outside function:", num)
In this example, num
is an integer object (which is immutable), and it is passed to the add_one
function. Inside the function, a copy of num
is created, and the copy is modified by adding 1
to it. However, the original num
object remains unchanged, and when the function is done, the modified copy is discarded.
- Mutable objects (like lists, dictionaries, sets) are passed by reference. This means that when you pass a mutable object to a function, the function receives a reference to the original object. Any changes made to the object inside the function will affect the original object.
Here’s an example to demonstrate how mutable objects are passed by reference:
def append_item(lst, item):
lst.append(item)
print("Inside function:", lst)
my_list = [1, 2, 3]
append_item(my_list, 4)
print("Outside function:", my_list)
In this example, my_list
is a list object (which is mutable), and it is passed to the append_item
function. Inside the function, the append
method is used to add an item to the original list. Since the function receives a reference to the original list, the changes made to the list inside the function also affect the original list.
In summary, whether arguments are passed by value or by reference in Python depends on the type of the argument. Immutable objects are passed by value, and mutable objects are passed by reference.