FAQs on the exercise Void — The Point of No Return
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Would have been helpful if you stated explicitly that you did not want us to print the quotation marks around the oscar wilde quote. I just spent ten minutes looking for a bug in my code that wasn’t there.
It would be nice to know that you don’t need to enter return before function because all previous examples had return marked as a blue operator before declared function. This was very off-putting!
When you define a function, nothing happens. So, when you define the oscar_wilde_quote() function and then run the program, you will see nothing being printed to the console.
It is when you actually call the function that the instructions within the function are executed. When (in your main function) you call the oscar function, that is when the std::cout statement within that oscar function is actually executed and you will see the quote being printed to the console.
Think of it like cooking. When you define a function, you are writing a recipe in your notebook but aren’t doing any cooking. When you call that function, then you are actually cooking by enacting the steps outlined in the recipe.
I don’t know what I’m doing wrong
I’ve been trying this lesson for half an hour, and nothing works. This is what I have so far
#include
// Define oscar_wilde_quote() below:
void oscar_wilde_quote(){
std::cout << “The highest, as the lowest, form of criticism is a mode of autobiography.\n”;
}
int main() {
// Call your function here:
}
If someone could tell me what I’m doing wrong and give the correct thing, I would appreciate it.
You have completed Step 1 of the exercise i.e. defining the function.
Step 2 asks you to call this function from main(). Note the comment // Call you function here: which points out where in main you are supposed to call the function you have defined. To call a function with no parameters, you just do function_name() where function_name should be replaced by the name of the function you are calling.
#include <iostream>
// Define oscar_wilde_quote() below:
std::string oscar_wilde_quote() {
std::string quote = "The highest, as the lowest, form of criticism is a mode of autobiography.\n";
return quote;
}
int main() {
// Call your function here:
std::cout << oscar_wilde_quote();
}
And this:
#include <iostream>
// Define oscar_wilde_quote() below:
void oscar_wilde_quote() {
std::cout << "The highest, as the lowest, form of criticism is a mode of autobiography.\n";
}
int main() {
// Call your function here:
oscar_wilde_quote();
}
?
I mean any difference with the performance or any other aspect, the result is the same.
If printing the quote to the terminal is the only objective, then both approaches will work. Indeed, the void function will make for more concise code. But, if we intend to do something with the quote like editing the quote or something else, then the function with return is more appropriate.
Also, suppose we wanted to print the quote 100 times. Then, the function with return would offer us better performance. From main(), we could make a single call to our function and save the result to a variable in main. Then, we could print the variable 100 times with a loop. With the void function, we would have to write a loop in main which would make 100 function calls. That would affect performance.
int main() {
// Calling the version of function which has return
std::string result = oscar_wilde_quote(); // Single call to function
for (int i = 0; i < 100; i++) {
std::cout << result;
}
}
as opposed to:
int main() {
// Calling the void version of function
for (int i = 0; i < 100; i++) {
oscar_wilde_quote(); // 100 calls to function
}
}