0% found this document useful (0 votes)
4 views5 pages

Understanding Python Arguments and Parameters

The document explains the difference between arguments and parameters in function calls, highlighting that arguments are the values passed to functions while parameters are the variables defined in the function. It also discusses types of arguments, including positional and keyword arguments, and introduces default parameters and variable scope, distinguishing between global and local variables. Additionally, it provides examples to illustrate these concepts and their implications in Python programming.

Uploaded by

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

Understanding Python Arguments and Parameters

The document explains the difference between arguments and parameters in function calls, highlighting that arguments are the values passed to functions while parameters are the variables defined in the function. It also discusses types of arguments, including positional and keyword arguments, and introduces default parameters and variable scope, distinguishing between global and local variables. Additionally, it provides examples to illustrate these concepts and their implications in Python programming.

Uploaded by

ankuyadav152
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ARGUMENTS VS PARAMETERS :

ARGUMENTS PARAMETERS
The values which are passed to the called The variables that receive the passed values
function from the calling block are known in the called function definition are known
as arguments. as parameters.
Arguments can be literals or expressions or The parameters always be variables. These
variables. cannot be literals or expressions.
Arguments appear in function calling Parameters appear in function header.
statement.

Example :
def Nameage( name, age) :
print(“ Hi I am “, name)
print(“ My age is “, age)
#--------------------------------------------
Yr = 20
Nameage(“Prince”, Yr)

In above python code, variables name and age are parameters whereas literal “Prince” and
variable Yr are arguments.

Python provides different ways of passing the arguments during the function call.
Types of arguments
 Keyword-only argument ( Named arguments)
 Positional-only argument

1. POSITIONAL ARGUMENTS :
Arguments which are passed to the called function definition as per the
order/position of parameters specified in the function header are known as
positional arguments.
The first positional argument always needs to be listed first when the function
is called. The second positional argument needs to be listed second and the
third positional argument listed third, etc.
We should use positional Arguments whenever we know the order of argument to
be passed.
If we change the argument position then we may get the unexpected output.

Example :

def nameAge(name, age):


print("Hi, I am", name)
print("My age is ", age)
#------------------------------------------------------------------------------------
# You will get correct output because argument is given in order
print("Case-1:")
nameAge("Prince", 20)
# You will get incorrect output because argument is not in order
print("\nCase-2:")
nameAge(20, "Prince")

Output :
Case-1:
Hi, I am Prince
My age is 20
Case-2:
Hi, I am 20
My age is Prince

2. Keyword/Named Arguments :
When parameter’s name is specified along with the argument, it is known as
keyword or named argument.
Syntax : <parameter’s name > = Argument
If we change the position of arguments then there will be no change in the
output.

Example :
def nameAge(name, age):
print("Hi, I am", name)
print("My age is ", age)

nameAge(name="Prince", age=20)

nameAge(age=20, name="Prince")
Output :
Hi , I am Prince
My age is 20
Hi, I am Prince
My age is 20

Benefits of using Keyword arguments over positional arguments


 On using keyword arguments you will get the correct output because the order
of argument doesn’t matter provided the logic of your code is correct.
But in the case of positional arguments, you will get more than one output on
changing the order of the arguments.
Difference between the Keyword and Positional Argument

Keyword-Only Argument Positional-Only Argument

Parameter Names are used to pass Arguments are passed in the order of
the argument during the function call. parameters which is defined in the
function declaration.

Order of parameter Names can be Order of values cannot be changed to


changed to pass the argument (or avoid the unexpected output.
values).

Syntax :- Syntax:-
FunctionName(paramName=value,…) FunctionName(value1,value2,value3,….)

NOTE:

If we are using both positional arguments and keyword arguments in the function calling statement,
then we first write positional arguments and then the keyword arguments.

It means positional arguments cannot follow the keyword arguments.

Example :

Nameage(name = “Prince”, 20) # incorrect

DEFAULT PARAMETERS :
Python allows assigning a default value to the parameter. Such parameters which are
assigned a default value at the time of defining a function are called default parameters.
A default value is a value that is predecided and assigned to the parameter when the
function call does not have its corresponding argument.
Example :
def Interest(P = 5000, R = 3, T = 2 ) :
SI = P*R*T/100
return(SI)
#----------------------------------------------------------------------------
I = Interest()
I = Interest(10000)
I = Interest(10000,5)
I = Interest(10000,5,8)

NOTE :
The default parameters must be the trailing parameters in the function header that means if
any parameter is having default value then all the other parameters to its right must also
have default values.
Example :
#incorrect as default must be the last #parameter
def Interest(principal = 1000, rate, time = 5):
#correct
def Interest(rate,principal = 1000, time = 5):

SCOPE OF VARIABLE :

The part of the program where a Variable is accessible can be defined as the scope
of that variable. A variable can have one of the following two scopes:

1. Global Scope
2. Local Scope

A variable that has global scope is known as a global variable.


A variable that has local scope is known as a global variable

Difference between the Global and Local Variables.

Global Variable Local Variable


A variable that is defined outside any A variable that is defined inside any
function or any block is known as a function or a block is known as a local
global variable. variable.
It can be accessed in any functions It can be accessed only in the function
defined onwards. or a block where it is defined.
Any change made to the global variable Any change made to the local variable
will impact all the functions in the will not impact other functions in the
program where that variable can be program.
accessed.
It exists till the program is executes. It exists only till the function executes.

Example :

A = 10 # A is a Global Variable
def FunctionA() :
B = 20 # B & C are Local Variables
C=A+B # Accessing Global Variable
print(“A = “, A)
print(“B = “, B)
print(“C = “, C)
#---------------------------------
print(“B = “, B) # Error as B is a local Variable
print(“A = “, A) # Accessing Global Variable

Note:
• Any modification to global variable is permanent and affects all the functions where it is
used.
• If a variable with the same name as the global variable is defined inside a function, then it
is considered local to that function and hides the global variable.
Example :
A = 10
def FunctionA() :
A = 15
B = 10
C=A+B
print(“A = “, A)
print(“C = “, C)
#-------------------------------------
FunctionA()
print(“A = “, A)

Output :
A = 15
C = 25
A = 10

• If the modified value of a global variable is to be used outside the function, then the
keyword global should be prefixed to the variable name in the function.
i.e. global A

Example :
A = 10
def FunctionA() :
global A
A = 15
B = 10
C=A+B
print(“A = “, A)
print(“C = “, C)
#-------------------------------------
FunctionA()
print(“A = “, A)

Output :
A = 15
C = 25
A = 15

Note : The following code will give error :


A = 10
def FunctionA() :
B = 10
C=A+B # ERROR : UnboundLocalError : ‘A’ is not associated with a value
A=A+5
print(“A = “, A)
print(“C = “, C)
#-------------------------------------
FunctionA()
print(“A = “, A)

You might also like