<< Chapter < Page Chapter >> Page >

If a parameter is passed by reference, the incoming parameter can be used by code in the function to modify the original. Some programming languages supportboth types of parameter passing.

According to tutorialspoint -- Python Functions , all parameters in Python are passed by reference . On the other hand, according to The Python Tutorial -- Defining Functions ,

"thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object)."

In this case, I come down on the side of call by value . I think it is more correct to say that parameters are passed by value and as a result, thefunction receives copies of references to objects. Even though I can use a copy of an object's reference to modify the object, I cannot use a copy of thatreference to cause the original reference to point to a different object. I can modify the object to which the reference points, but I cannot modify thereference itself.

I will illustrate what I mean by this in conjunction with the program in Listing 1 .

Discussion and sample code

This module will examine the following kinds of arguments:

  • Required arguments
  • Default arguments
  • Keyword arguments
  • Variable-length arguments

Passing by value or reference

The program shown in Listing 1 illustrates the significance of passing parameters by value or by reference when those parameters are references to objects. Listing 1 also illustrates the use of two required arguments .

Listing 1 . A list-modifier function.
# Illustrates pass by value or reference #---------------------------------------------------def listModifier(listA,listB): """Illustrates pass by value or reference"""print("In listModifier") print("Use incoming parameter to append to listA")listA.append(3.14159) print("New listA = " + str(listA))print("Assign a new list to listB") listB = ["A","new","list"]print("New listB = " + str(listB)) return#End function definition #Call the functionprint("Create two lists") aList = ["ab","cd","ef"]bList = ["The","old","list"] print("aList = " + str(aList))print("bList = " + str(bList)) print("Call listModifier")listModifier(aList,bList) print("Back from listModifier")print("aList = " + str(aList)) print("bList = " + str(bList))

Figure 1 shows the output produced by the code in Listing 1 .

Figure 1 . Output from the code in Listing 1.
Create two lists aList = ['ab', 'cd', 'ef']bList = ['The', 'old', 'list'] Call listModifierIn listModifier Use incoming parameter to append to listANew listA = ['ab', 'cd', 'ef', 3.14159] Assign a new list to listBNew listB = ['A', 'new', 'list'] Back from listModifieraList = ['ab', 'cd', 'ef', 3.14159] bList = ['The', 'old', 'list']

The function definition

The code in Listing 1 defines a function named listModifier . This function receives two incoming parameters. Each parameter points to adifferent list. I contend that the incoming parameters are actually copies of the variables that were passed as parameters.

The code in the function uses one of the incoming parameters to access the list and to append a value of 3.14159 onto the end of the list. The second line of text from the bottom of Figure 1 confirms that this operation was successful.

Questions & Answers

Why is b in the answer
Dahsolar Reply
how do you work it out?
Brad Reply
answer
Ernest
heheheehe
Nitin
(Pcos∅+qsin∅)/(pcos∅-psin∅)
John Reply
how to do that?
Rosemary Reply
what is it about?
Amoah
how to answer the activity
Chabelita Reply
how to solve the activity
Chabelita
solve for X,,4^X-6(2^)-16=0
Alieu Reply
x4xminus 2
Lominate
sobhan Singh jina uniwarcity tignomatry ka long answers tile questions
harish Reply
t he silly nut company makes two mixtures of nuts: mixture a and mixture b. a pound of mixture a contains 12 oz of peanuts, 3 oz of almonds and 1 oz of cashews and sells for $4. a pound of mixture b contains 12 oz of peanuts, 2 oz of almonds and 2 oz of cashews and sells for $5. the company has 1080
ZAHRO Reply
If  , , are the roots of the equation 3 2 0, x px qx r     Find the value of 1  .
Swetha Reply
Parts of a pole were painted red, blue and yellow. 3/5 of the pole was red and 7/8 was painted blue. What part was painted yellow?
Patrick Reply
Parts of the pole was painted red, blue and yellow. 3 /5 of the pole was red and 7 /8 was painted blue. What part was painted yellow?
Patrick
how I can simplify algebraic expressions
Katleho Reply
Lairene and Mae are joking that their combined ages equal Sam’s age. If Lairene is twice Mae’s age and Sam is 69 yrs old, what are Lairene’s and Mae’s ages?
Mary Reply
23yrs
Yeboah
lairenea's age is 23yrs
ACKA
hy
Katleho
Ello everyone
Katleho
Laurene is 46 yrs and Mae is 23 is
Solomon
hey people
christopher
age does not matter
christopher
solve for X, 4^x-6(2*)-16=0
Alieu
prove`x^3-3x-2cosA=0 (-π<A<=π
Mayank Reply
create a lesson plan about this lesson
Rose Reply
Excusme but what are you wrot?
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask