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

how do you get the 2/50
Abba Reply
number of sport play by 50 student construct discrete data
Aminu Reply
width of the frangebany leaves on how to write a introduction
Theresa Reply
Solve the mean of variance
Veronica Reply
Step 1: Find the mean. To find the mean, add up all the scores, then divide them by the number of scores. ... Step 2: Find each score's deviation from the mean. ... Step 3: Square each deviation from the mean. ... Step 4: Find the sum of squares. ... Step 5: Divide the sum of squares by n – 1 or N.
kenneth
what is error
Yakuba Reply
Is mistake done to something
Vutshila
Hy
anas
hy
What is the life teble
anas
hy
Jibrin
statistics is the analyzing of data
Tajudeen Reply
what is statics?
Zelalem Reply
how do you calculate mean
Gloria Reply
diveving the sum if all values
Shaynaynay
let A1,A2 and A3 events be independent,show that (A1)^c, (A2)^c and (A3)^c are independent?
Fisaye Reply
what is statistics
Akhisani Reply
data collected all over the world
Shaynaynay
construct a less than and more than table
Imad Reply
The sample of 16 students is taken. The average age in the sample was 22 years with astandard deviation of 6 years. Construct a 95% confidence interval for the age of the population.
Aschalew Reply
Bhartdarshan' is an internet-based travel agency wherein customer can see videos of the cities they plant to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400 a. what is the probability of getting more than 12,000 hits? b. what is the probability of getting fewer than 9,000 hits?
Akshay Reply
Bhartdarshan'is an internet-based travel agency wherein customer can see videos of the cities they plan to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400. a. What is the probability of getting more than 12,000 hits
Akshay
1
Bright
Sorry i want to learn more about this question
Bright
Someone help
Bright
a= 0.20233 b=0.3384
Sufiyan
a
Shaynaynay
How do I interpret level of significance?
Mohd Reply
It depends on your business problem or in Machine Learning you could use ROC- AUC cruve to decide the threshold value
Shivam
how skewness and kurtosis are used in statistics
Owen Reply
yes what is it
Taneeya
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