Argument Passing methods in python


Argument Passing Methods

Pass means to provide arguments for the functions, and the arguments contain information.

Passing Arguments can be done in two ways in Python:-

  • Call by Assignment -

      When you pass arguments like whole numbers (int, float, etc.), strings, or tuple data type to a function, the passing of the function is like call-by-value because the user cannot modify the value of the non-changeable objects(immutable) being passed to the function. If we try to modify the function, this will not affect the passed value inside the argument. We can use pass by Assignment only when we want the argument to be fixed and not change during the entire program. The values of the variable, once stored into their dummies, can not be changed.

  • Call by Object Reference -

      Whereas passing changeable or mutable objects (list) to the function can be considered call-by-reference because when their values are changed inside the function passed, it will also be changed outside the function.

      Assignment pass can not provide any changes to the defined arguments, but if the values can be defined through any reference, we can change the value with the help of reference. If we can perform any operation on the function, it will directly affect the variable to which it refers as the argument is referenced.

      Suppose we can perform any function operation on the variable. In that case, its initial address will not change, but if we use operators or reassign the variable, then the initial address will change.

  • We can return multiple values with the help pass by reference.

Function Passing

Python uses a specific system, which is known as “Call by Object Reference” or “Call by assignment”.

  • When you pass arguments like whole numbers (int, float, etc), strings, or tuple data type to a function, the passing of the function is like call-by-value because the user cannot modify the value of the non-changeable objects(immutable) being passed to the function.
  • Whereas passing changeable or mutable objects (list) to the function can be considered as call-by-reference because when their values are changed inside of the function passed, then it will also be changed outside the function.

Example:

def my_function(fname): print(fname + " website") x='1knowledge2life' y='2knowledge2life' z='3knowledge2life' my_function(x) my_function(y) my_function(z)

OUTPUT:

1knowledge2life website
2knowledge2life website
3knowledge2life website