Question

In: Computer Science

It is a C++ introduction exercise and I am a rookie. Please contain detailed reasons and...

It is a C++ introduction exercise and I am a rookie. Please contain detailed reasons and explanations and I would appreciate any help you can provide.

When we pass parameters to functions by value or by reference, we're doing different things; the end result will not necessarily be the same. And, of course, it's wise for us to understand exactly how the results will be different. But another angle we need to consider is why we need to have these two different kinds of parameters, which is to say that we should understand when we would be better off using one technique instead of the other.

1. What are the design benefits of passing a parameter by value? What can you do when you pass by value that you can't do when you pass by reference?

2. What are the design benefits of passing a parameter by reference? What can you do when you pass by reference that you can't do when you pass by value?

3. Suppose you were going to write a function with one parameter, and that the result would be the same whether you passed the parameter by value or by reference — it's not always the case that the outcome will be different. In what circumstances would you expect the pass-by-value version to run faster? In what circumstances would you expect the pass-by-value version to use less memory?

4. Same situation as the previous question. In what circumstances would you expect the pass-by-reference version to run faster? In what circumstances would you expect the pass-by-reference version to use less memory?

Note that these questions are not about how these techniques work, they're about why you would prefer one rather than the other.

Solutions

Expert Solution

Explanation

In single argument case-Passed parameters by value or by reference, we are doing differentn things and the end result will not be the same a

1. benefit of passing by value

In call by value, original value is not modified.

2. benefit of passing by reference

Actual and formal arguments will be created in same memory location in pass by references

3. suppose we are going to write with one parameter, do it by value or reference

PASS BY REFERENCE

#include <iostream>  

using namespace std;  

void change(int* NUM)  

{  

*NUM = 5;  

}

int main()  

{  

int NUM = 3;  

change(&NUM);  

cout << "Value of the data is AFTER FUNCTION CALL: " << NUM<< endl;  

return 0;  

}

4. pass by reference

This method is also called as call by reference. This method is efficient in both time and space.

  

PASS BY VALUE

1. LIMITATION

Inefficiency in storage allocation

For objects and arrays, the copy semantics are costly

Conclusion:

Pass-by-reference makes some things more efficient and allows updates to parameters within subroutines.

where as pass-by-value is much easier to make memory safe and efficient. If you pass pointers/references to stack-allocated memory, you need to make sure the memory outlives the reference. If you instead use automatic memory management to ensure that, you pay a run-time price for that.


Related Solutions

Hello, this question relates to a class I am taking called introduction to C++. I have...
Hello, this question relates to a class I am taking called introduction to C++. I have no experience writing programs and outside of learning out of a textbook, and studying on my own, have little understanding of logic. I have been assigned a problem that requires me to write a program for a Grocery Bill, where the consumer inputs the price for 5 items, that the program calculates the total with a 6% sales tax. I really am not sure...
please answer the following detailed in a paragraph. I am really struggling with this concept. thanks!...
please answer the following detailed in a paragraph. I am really struggling with this concept. thanks! Microorganisms can thrive under many different conditions, including high-temperature environments such as hot springs. To function properly, cell membranes have to be in a fluid state. How do you expect the fatty acid content (saturated versus unsaturated) of bacteria living in high-temperature environments might compare with that of bacteria living in more moderate temperatures? 2. Heating a protein sufficiently may cause it to denature....
I am working on exercise 5.48 from Introduction to Computing using python (Author: Perkovic). Problem Question:...
I am working on exercise 5.48 from Introduction to Computing using python (Author: Perkovic). Problem Question: Let list1 and list2 be two lists of integers. We say that list1 is a sublist of list2 if the elements in list1 appear in list 2 in the same order as they appear in list1, but not necessarily consecutively. For ex, if list1 is defined as [15,1,100] and list2 is defined as [20,15,30,50,1,100]. Then list 1 is a sublist of list 2 because...
I am teaching other students so please be detailed--explain what is important, answer the questions completely,...
I am teaching other students so please be detailed--explain what is important, answer the questions completely, as if I were reviewing the material for myself alone! APA formatting of posts is not necessary. APA formatting of citations/references is necessary. :0) Beta agonists are commonly prescribed to patients with breathing problems. How would they work in the lungs?                               How would they impact the eyes, heart, liver, GI tract?                              What would those effects be and would they be considered side effects or...
I am currently completing an assignment for an introduction for accounting. I have been asked to...
I am currently completing an assignment for an introduction for accounting. I have been asked to complete a general journal for the following transactions: August 2           Sahra paid $30 from the business bank account for dinner at ‘Waves’ a beachside café. August 3           Deep Sea Cleaning Co cleaned the shop and workshop and left an invoice for $195 on the counter. August 6           A new range of SPF fabric was purchased from ‘World Fabrics Ltd’ for $6,200. A part-payment of $200 was paid...
Please, this is the fifth time I am posting this question, all the answers I am...
Please, this is the fifth time I am posting this question, all the answers I am getting are inaccurate because the experts are not putting all value into account like the loan interest rate, tax rate, depreciation, annual cash outflows, salvage value, annual cash inflows, and discount rate. please also support the answer with a full interpretation and the step by step approach in solving the answer. thank you:) In this paper, please discuss the following case study. In doing...
C++ Problem. I am providing the code. Just Please provide the new function and highlight it....
C++ Problem. I am providing the code. Just Please provide the new function and highlight it. implement the functions replaceAt, seqSearch, and remove. Test your new function too in main. Also, Test Old functions in main. Show the output. Also, modify the functions accordingly which have "See Programming Exercise 22". main.cpp : #include <iostream> using namespace std; #include "arrayListTypetempl.h" int main(){    arrayListType<int> intList;    arrayListType<char> charList;       intList.insertEnd(5);    intList.insertEnd(3);    intList.insertEnd(4);    intList.insertEnd(55);       charList.insertEnd('a');   ...
i am student of BBA and i am making an internship please give me the answer...
i am student of BBA and i am making an internship please give me the answer Company: ABU DHABI POLICE UAE Job Position: Police Officer 1. Duties and Responsibilities (200 words) 2. My Experience (100 words) 3. Internship Lesson (5 lesson) (100 words) 4. Changes Would You Make in Internship (100 words)
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting)...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting) to determine the day of your next meeting from today. The program reads from the user an integer value representing today’s day (assume 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, etc…) and another integer value representing the number of days to the meeting day. The program determines and prints out the meeting day. Format the outputs following the sample...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT