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
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following commands do: dd, y3, p, :set cindent (1 pt) VIM exercises These exercises on the computer need to be repeated by each student in the pair. This is to ensure that both students understand how to get around in Linux!!! For this part of the lab, you will create a .vimrc file that will help you develop your C++ programs using VIM. First, we...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
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...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...
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...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
Part II - Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will...
Part II - Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will find dozens of alien conspiracy shows that reference Pi as something so advanced that it must be alien.  The number πis a mathematical constant, the ratio of a circle's circumference to its diameter, commonly approximated as 3.14159. So you could graph the value or calculate an approximate value using the series given below: Pi = 4 * (1/1 – 1/3 + 1/5 – 1/7 +...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT