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
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...
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...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
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 >=...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT