Question

A function can return a value of type T in one of several different ways. Which...

  1. A function can return a value of type T in one of several different ways. Which of these is correct? If correct, give a short declaration of a function that returns its value by these mechanism. Comment on the const methods of function return value. Comment on when there is danger involved in returning a reference of any kind from a function. Finally, which one of these returns an l-value and which an r-value?
    1. By value
    2. By lazy evaluation.
    3. By reference
    4. By const value
    5. By const reference

Homework Answers

Answer #1

A function can return value a value of type T in one of several different ways . Which one these is correct

All of them are correct.

Give a short declaration of a function that returns its value by these mechanism:

1. By value

T test(T t)

{

return t;

}

2. By const value

  

const T test(const T t)

{

...

return t;

}

3. By reference

T& test(T& t)

{

...

return *t;

}

4. By const reference

const T& test(const T& t)

{

...

return *t;

}

5. By lazy evaluation

T get()

{

if (calculated)

{

return result;

}

calculated = true;

return result = function();

}

Comment on const method of function return value

A function becomes const when the const keyword is used in the function’s declaration. The idea of const functions is not to allow them to modify the object on which they are called.

A const member function can be called by any type of object. Non-const functions can be called by non-const objects only.

Here is the syntax of const member function in C++ language,

datatype function_name const();
In the below code we have made i as constant, hence if we try to change its value, we will get compile time error. Though we can use it for substitution for other variables.

int main
{
const int i = 17;
const int j = i + 17;
i++;
}

Comment on where there is danger involved in returning a reference of any kind from a function.

Never return any kind of reference of a local / Temporary variable.

Which one of these returns and L value and which  is an R value.

by value -- R-value

lazy evalution-- It can return L-value or R-value (based on implementation)

reference-- L-value

const value-- R-value

const reference--- L-value

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
The strength of one's immune system can be evaluated in several ways. One of the most...
The strength of one's immune system can be evaluated in several ways. One of the most popular methods involves measuring the concentration of adenosine triphosphate (ATP) in the blood. In women, the mean blood ATP level is 390 ng/mL, and the standard deviation is 115 ng/mL. It has been hypothesized that women with Major Depressive Disorder (MDD) have decreased immune function. A recent study found that a random sample of 70 women with MDD had an average blood ATP level...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output. Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by...
"Risk' can be best defined as on the of the followings:   a. Variability of returns and...
"Risk' can be best defined as on the of the followings:   a. Variability of returns and probability of financial loss b. Chance of financial loss   c. Variability of returns   d. Correlation of relationship among two variables Which of the following statement is NOT TRUE when we argue that the idea of riskless arbitrage is to accumulate the portfolio with following conditions : a. Requires no net wealth invested initially   b. Invest in the long-term securities only where risk will be...
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...
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...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
The Business Case for Agility “The battle is not always to the strongest, nor the race...
The Business Case for Agility “The battle is not always to the strongest, nor the race to the swiftest, but that’s the way to bet ’em!”  —C. Morgan Cofer In This Chapter This chapter discusses the business case for Agility, presenting six benefits for teams and the enterprise. It also describes a financial model that shows why incremental development works. Takeaways Agility is not just about the team. There are product-management, project-management, and technical issues beyond the team’s control. Lean-Agile provides...
Discuss ethical issues that can be identified in this case and the mode of managing ethics...
Discuss ethical issues that can be identified in this case and the mode of managing ethics Enron finds itself in this case. How would you describe the ethical culture and levels of trust at Enron? Provide reasons for your assessment. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT