0% found this document useful (0 votes)
62 views8 pages

Passing Values To Functions: Unit - I Chapter - Ii

The document discusses different ways of passing values to functions in Python: 1. Pass by reference (call by reference) - All parameters in Python are passed by reference, meaning any changes made to the parameter inside the function will be reflected outside the function. 2. Pass by value (call by value) - For immutable objects like integers and strings, passing acts like call by value where the object reference is passed but the objects can't be changed. Mutable objects like lists can be changed in-place inside the function. 3. There are four types of arguments in Python functions: required arguments which must be provided, default arguments which have set default values, keyword arguments which allow specifying the name of each

Uploaded by

caspindafnigovz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views8 pages

Passing Values To Functions: Unit - I Chapter - Ii

The document discusses different ways of passing values to functions in Python: 1. Pass by reference (call by reference) - All parameters in Python are passed by reference, meaning any changes made to the parameter inside the function will be reflected outside the function. 2. Pass by value (call by value) - For immutable objects like integers and strings, passing acts like call by value where the object reference is passed but the objects can't be changed. Mutable objects like lists can be changed in-place inside the function. 3. There are four types of arguments in Python functions: required arguments which must be provided, default arguments which have set default values, keyword arguments which allow specifying the name of each

Uploaded by

caspindafnigovz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SISHYA SCHOOL, HOSUR

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

XII- COMPUTER SCIENCE Page 1


SISHYA SCHOOL, HOSUR

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

XII- COMPUTER SCIENCE Page 2


SISHYA SCHOOL, HOSUR

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.

2. Binding Names to Objects

In python, each variable to which we assign a value/container is treated as an


object. When we are assigning a value to a variable, we are actually binding a name to
an object.
Example 1

Output

XII- COMPUTER SCIENCE Page 3


SISHYA SCHOOL, HOSUR

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

XII- COMPUTER SCIENCE Page 4


SISHYA SCHOOL, HOSUR

1. Python Required Arguments:


As the name suggests, These are the mandatory arguments those needs to be
provided while calling the function. Number of arguments provided should exactly match
to the number of arguments defined in function definition. If these arguments are not
provided while calling the function it will result an error.
Example 1: Python program for required 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.

XII- COMPUTER SCIENCE Page 5


SISHYA SCHOOL, HOSUR

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.

XII- COMPUTER SCIENCE Page 6


SISHYA SCHOOL, HOSUR

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.

3. Python Keyword Arguments:

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.

However in python it is possible to assign specific value to a specific argument only,


No matter in what sequence you are giving the input. In such cases keyword arguments are
used. In this type of argument we do specify the argument name along with its value. So the
change in sequence doesn’t affect the values assignment.

See the example below to understand python keyword argument.

Example 3

Output

XII- COMPUTER SCIENCE Page 7


SISHYA SCHOOL, HOSUR

4. Python variable length (Arbitrary) Arguments

In some situation, While defining a function we can’t predict the number of


arguments user is going to pass while calling a function.

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

XII- COMPUTER SCIENCE Page 8

You might also like