Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Variables, Expression and...
  5. Function
  6. Parameter Passing

Parameter Passing

  • In python, every variable name is a reference
  • When a variable is passed to a function, a new reference to the object is created
  • If the value passed in a function is immutable, the function does not modify the caller’s variable
  • If the value is mutable, the function modify the caller’s variable in-place
def changing_values(x,y,z):
    x = 10
    y.remove(20)
    z=[5,10,15]
    print("Inside the function, values of a,b,c are")
    print(x)
    print(y)
    print(z)
a=100
b=[10,20,30]
c=[1000]
print("Before calling the function: values of a,b,c are")
print(a)
print(b)
print(c)
changing_values(a,b,c)
print("After calling the function: values of a,b,c are")
print(a)
print(b)
print(c)

Output:

Before calling the function: values of a,b,c are
100
[10, 20, 30]
[1000]
Inside the function, values of a,b,c are
10
[10, 30]
[5, 10, 15]
After calling the function: values of a,b,c are
100
[10, 30]
[1000]

Loading

Views: 2

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments