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!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I would like to ask about the fuctions. Okay if;
“void something(std::string 1, std::string 2)” why does, “double something(double 1, double 2)”.
What’s the differences?
Is void is only for std::string and double is the same as void but only for double, so what does bool is for and what is the “void” for integer?
Is it something like this? I’m so confused.
You can see that double from the answer is a double function. Your double function is a void. Void returns nothing, yet you are attempting to return average_var.
It will be easier to read it like this and compare where you went wrong.
Actually, there is a reason. The ‘void’ function only returns strings, nothing else. In order to return a double, integer, or boolean, you need to have separate code for each. ‘void’ only refers to strings and
std::cout << something1 << something 2;
@psmilliorn is correct. A void function doesn’t return anything.
When you run your code, the statements you have written are executed in sequence line by line. If you call a function, you make a brief excursion to that function, carry out the statements written within that function, and then come back to wherever you left off in your main program. If your function doesn’t need to send any information back to the main program, then you declare the function’s return type as void i.e. the function isn’t meant to return any value. Even if within your function, you are producing some output such as writing a string or number to the console via a std::cout statement, you aren’t sending any information back to the main program so void captures this intent.
On the other hand, if you want that function to send some information back to your main program, you can accomplish this by using a return statement in your function. This return statement can be of any type, it could be int, double, string, vector… The return type of the function doesn’t have to match the parameter types. The return type will depend on what is the purpose/goal of the function.
For example, in the challenge we are asked to write a function which calculates the average of two numbers (of type double). Since, we want to communicate this average value (which also will be of type double because of the mathematics) back to the main program, so we define the function as double average(double num1, double num2) and we will also make sure to include a return statement within our function. In our main function, we will make sure to store this returned value in some variable.
If we didn’t want to save the average value but only print out the average, then we could define the function as void average(double num1, double num2). In this case, we won’t include a return statement within our average function, but will just print the average value to the console via a std::cout statement so that our function does something meaningful. The main program won’t have access to this calculated average value, because a void function doesn’t send any value back.
If we wanted to calculate the average but instead of returning a number, we wanted to communicate the result to the main program in string form, we could declare the function as std::string average(double num1, double num2). Within the function, we could do something like
std::string average(double num1, double num2) {
double avg = (num1 + num2) / 2.0;
return "The average of the numbers is " + std::to_string(avg) + "\n";
}
So, the return type doesn’t have to match the parameter type. The return type depends on what you are trying to send back and in what format.
Perhaps it has to do with the quotes around the newline character in your cout statements in main.
In the code that you have pasted, you have: “\n”
It can happen sometimes when copying code.
If you type the quotes in yourself, you should have: "\n" which is recognized by the compiler.
I just wanted to say that the checks on this question are a bit too picky, my answer was:
double average ( double num1, double num2) {
return (num1+num2)/2;
}
and it wouldn’t accept it because it wanted some more white space to look like this:
double average(double num1, double num2) {
return (num1 + num2) / 2;
}
don’t you think this is too picky? my solution caused no errors and returned 33 & 1.5 just like the solution did, so I don’t think it needs to be so exact. it was even bothering me because my closing bracket was on line 6 and not line 8. its a bit frustrating.