<< 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.

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