Question

Q. Name and differentiate the following two constructs: For (auto e: myContainer) and for (auto &e:...

Q. Name and differentiate the following two constructs:
For (auto e: myContainer) and for (auto &e: myContainer)
Explain the situation where one construct is preferred to the other.
Give an example usage.

I got the following answer here at Chegg but I still need an example (simple one) please:
1st one is call by value when you want use it for read only purpose than use this cal by value
2nd one is call by reference. we will use this when we want to modify the parameters Usage:
we will use call by reference when we want to return the mulitple values from a function

Homework Answers

Answer #1

Call by value:

In this, only the value of a parameter is passed, and not the parameter itself. We may change the value in any way we like but it won't affect the original parameter.

For example, consider the below snippet.

void func(int n)

{

//function workings

}

main()

{

  int m = 5, p;

p = func(m);

}

In this, only the value of m is passed to func, and not the address. Suppose m is stored at position 2001 in the memory. Thus, 2001 has the value 5 in it. In call by value, only this 5 is passed, and not 2001. Thus, the variable n in func maybe in position 7867. Modifying n in func will change the value at memory position of 7867, but not 2001. Thus the original parameter will remain unchanged.

Call by reference:

Unlike call by value, the memory address itself is passed to the function. As the memory address has the value of the original parameter, any change in the function will reflect on the original.

For example, consider the below snippet.

void func(int &n)

{

//function workings

}

main()

{

int m = 5, p;

p = func(&m)

}

In this, the address of m is passed to the function. Again, suppose m is stored at memory address 2001. In call by reference, this 2001 itself is passed to the function. So n has value 2001. Thus, changing anything will change the value at address 2001, and thus the original parameter will be changed.

When to use which?

Call by value is mainly used to read and perform operations with the data. On the other hand, call by reference is used when along with reading and performing an operation, we also need to write something in the parameter. However, we must be careful while using call by reference because otherwise any error may cause data corruption.

In case of any doubt, drop a comment and I'll surely get back to you.

Please give a like if you're satisfied with the answer. Thank you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete the following activities and then post your responses in the Activity #5 discussion forum (link...
Complete the following activities and then post your responses in the Activity #5 discussion forum (link below). Consider a short multiple choice quiz with three items, and each item has four choices (only one of the choices is correct). Suppose that you are taking this quiz but you are completely and utterly unprepared for it. That means that you only option for the quiz is to guess the answers. Suppose you are thinking about the first item: what's the probability...
Hypothesis testing is the basis of inferential statistics. Statisticians are always coming up with new tests...
Hypothesis testing is the basis of inferential statistics. Statisticians are always coming up with new tests and testing new characteristics of population parameters. One of the simplest tests that currently exist is the one-sample test for means. A random sample is drawn. If the population variance is known, then we use the Z test; if the population variance is unknown, we use the T test. In addition, there are some additional assumptions that can be made. For example, if a...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Case#1: Develop a personal sense of time Keeping a time log One important way by which...
Case#1: Develop a personal sense of time Keeping a time log One important way by which you can develop a personal sense of time is to keep a time log, recording how your time is actually spent over a period of say, a week. The principle of a time log or audit is to divide each day for the next week or two into fteen minute intervals.At the end of each hour record how the previous hour was spent. Keeping...
There are two reflective essays from MED students during their third year internal medicine clerkship. One...
There are two reflective essays from MED students during their third year internal medicine clerkship. One student sees each connection to a patient as like the individual brush strokes of an artist and the other sees gratitude in a patient with an incurable illness and is moved to gratitude in her own life. (WORD COUNT 500) Reflect on both essays and then choose one and describe how the student grew from the experience. Then explain what you learned as a...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
Read the LDC Cloud Systems Case Study and answer the question below: THE EMAIL The email...
Read the LDC Cloud Systems Case Study and answer the question below: THE EMAIL The email had been sent from one mid-level accounting manager at headquarters to another about six months earlier. The message read: Bill, here are the accounting issues I mentioned, and as you know, some of these go back a ways. The data that supports these accounts are not always clear and keep changing, and we are not sure what the correct accounting treatment is for these...
3) Now you will plan for your retirement. To do this we need to first determine...
3) Now you will plan for your retirement. To do this we need to first determine a couple of values. a. How much will you invest each year? $1,000 a year is what i will invest. State what you will use for P, r, and n to earn credit. ( The typical example of a retirement investment is an I.R.A., an Individual Retirement Account, although other options are available. However, for this example, we will assume that you are investing...
Which of the following correctly applies capitalization rules? Group of answer choices I heard Jay say...
Which of the following correctly applies capitalization rules? Group of answer choices I heard Jay say that he “Wants to be transferred to our Houston office.” Our midwest region is performing better than any of us dreamed possible. He will finish his Bachelor’s degree in May. What about his education? His experience? His potential for promotion? Flag this Question Question 61 pts Which of the following sentences demonstrates correct word usage? Group of answer choices Your involvement with drugs in...
Please read the article below, and answer the questions that follow. In doing so, remember the...
Please read the article below, and answer the questions that follow. In doing so, remember the following, - Although the use of generic theories, covered in class/your module guide/text book will provide a framework, the expectation for the student is to be able to discuss it in context with research relevant to South Africa and your own reflective experiences. - You are required to extensively research current South African trends, amidst our changing political landscape and benchmarked against a global...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT