Passing Values To Functions: Unit - I Chapter - Ii
Passing Values To Functions: Unit - I Chapter - Ii
UNIT –I
CHAPTER – II
PASSING VALUES TO FUNCTIONS
We can pass mutable/immutable objects to python function by two ways:
1. Pass by Reference (OR) Call by Reference
2. Pass by Value (OR) Call by Value
Pass by Reference:
In call-by-reference evaluation, which is also known as pass-by-reference, a function
gets an implicit reference to the argument, rather than a copy of its value. As a
consequence, the function can modify the argument, i.e. the value of the variable in the
caller's scope can be changed. The advantage of call-by-reference consists in the advantage
of greater time- and space-efficiency, because arguments do not need to be copied. On the
other hand this harbours the disadvantage that variables can be "accidentally" changed in a
function call. So special care has to be taken to "protect" the values, which shouldn't be
changed.
All parameters (arguments) in the Python language are passed by reference. It
means if you change what a parameter refers to within a function, the change also reflects
back in the calling function
Example
Output
Pass by value
The most common strategy is the call-by-value evaluation, sometimes also called
pass-by-value. This strategy is used in C and C++, for example. In call-by-value, the
argument expression is evaluated, and the result of this evaluation is bound to the
corresponding variable in the function. So, if the expression is a variable, a local copy of its
value will be used, i.e. the variable in the caller's scope will be unchanged when the
function returns.
Example
Output
If you pass immutable arguments like integers, strings or tuples to a function, the
passing acts like call-by-value. The object reference is passed to the function parameters.
They can't be changed within the function, because they can't be changed at all, i.e. they are
immutable. It's different, if we pass mutable arguments. They are also passed by object
reference, but they can be changed in place in the function. If we pass a list to a function, we
have to consider two cases: Elements of a list can be changed in place, i.e. the list will be
changed even in the caller's scope. If a new list is assigned to the name, the old list will not
be affected, i.e. the list in the caller's scope will remain untouched.
Output
Example 2
Output
The output of the above two examples are different because the list is mutable and
the string is immutable. An immutable variable cannot be changed once created. If we wish
to change an immutable variable, such as a string, we must create a new instance and bind
the variable to the new instance. Whereas, mutable variable can be changed in place.
3. Types of arguments
Arguments are used to call a function. There are basic four types of python function
arguments;
1. Required Argument
2. Default Arguments
3. Keyword Arguments
4. Variable length (Arbitrary) Arguments
Output
Here in above example, num1 and num2 are the mandatory arguments. In Result 2
we can see that if we don’t provided the required arguments it results into the error.
So in simple words, while calling a function with required arguments the argument
type and number should exactly match with the arguments specified in the function
definition.
2. Python default Arguments:
These type of arguments has default value given in function definition itself. It takes
the default value if user does not provide the argument value while calling the function.
On the other hand if user provides the value to default type of argument then default
value gets over written by the provided value.
Example 2: Python Default Arguments
Output
In above example, While defining function “area” we have defined argument “breadth” with
default value as “5”. Now we are calling the function “area” twice in the code.
first time we are calling the area function by providing the breadth argument value “7”. So
here it overrides the default value and takes the user provided input into consideration. So
the total area calculated is 35.
Second time we have not provided the breadth argument value. So it takes ‘breadth=5’ as
the default argument value. Therefore the area is 25.
When we try to call a function with multiple arguments. The values provided in
function call (in parenthesis) gets assigned to the arguments according to the order in
which they are provided. If we alter the sequence of values then assignment also changes.
Example 3
Output
So when we are uncertain about the number of argument then such a situation can
be handled by using python variable length or arbitrary arguments. We can provide
multiple values to an arbitrary arguments while calling a function.
If we want to make any argument arbitrary then while defining the new function we
have to mark an asterisk (*) mark in front of argument. In the function definition any
argument with asterisk (*) mark before is considered as arbitrary argument.
Example 4
Output