FAQ: Code Challenge: C++ Functions - Average

This community-built FAQ covers the “Average” exercise from the lesson “Code Challenge: C++ Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Average

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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.

3 Likes

I got this:

#include

// Define average() here:
double average(double num1, double num2)
{
return (num1+num2)/2;
}

int main() {

std::cout << average(42.0, 24.0) << “\n”;
std::cout << average(1.0, 2.0) << “\n”;

}

namespace “std” has no member “cout”
You are missing an include.

double average(double num1, double num2)
{
    return (num1 + num2) / 2;
}

int main()
{

    std::cout << average(42.0, 24.0) << "\n";
    std::cout << average(1.0, 2.0) << "\n";
}

I have a question reg the average exercise. The code i wrote was this. what is the difference from it to the correct answer?

My code:
#include

// Define average() here:

void average(double num1, double num2){
double average_var;
average_var = (num1 + num2) / 2;
return average_var;
}

int main() {
std::cout << average(42.0, 24.0) << “\n”;
std::cout << average(1.0, 2.0) << “\n”;
}

ANSWER FROM EXERCISE:
#include

// Define average() here:
double average(double num1, double num2) {
return (num1 + num2) / 2;
}

int main() {
std::cout << average(42.0, 24.0) << “\n”;
std::cout << average(1.0, 2.0) << “\n”;
}

When you format your code

#include

// Define average() here:

void average(double num1, double num2)
{
    double average_var;
    average_var = (num1 + num2) / 2;
    return average_var;
}

int main()
{
    std::cout << average(42.0, 24.0) << “\n”;
    std::cout << average(1.0, 2.0) << “\n”;
}

ANSWER FROM EXERCISE :
#include

    // Define average() here:
    double
    average(double num1, double num2)
{
    return (num1 + num2) / 2;
}

int main()
{
    std::cout << average(42.0, 24.0) << “\n”;
    std::cout << average(1.0, 2.0) << “\n”;
}

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.

#include

// Define average() here:

void average(double num1, double num2)
{
    double average_var;
    average_var = (num1 + num2) / 2;
    return average_var;
}

double average(double num1, double num2)
{
    return (num1 + num2) / 2;
}

int main()
{
    std::cout << average(42.0, 24.0) << “\n”;
    std::cout << average(1.0, 2.0) << “\n”;
}
1 Like

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;

Void returns nothing, that’s why its void. It’s void of a return.

1 Like

@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.

1 Like

Hey @wiki-bot, look at the photo. It just said, “Is average() defined?” It’s just giving a massive error in the console. Can you help?

Hey, I just discovered that it is just a spacing issue. Regards, @CodeAtharvMN

In your screenshot, you have used void.
The instructions specify “The function should return a double…”

#include

double average(double num1, double num2) {

return(num1 + num2) / 2;

}

int main() {

 std::cout << average(42.0, 24.0) << “\n”;

std::cout << average(1.0, 2.0) << “\n”;

}
I don’t know what am i doing wrong?

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.

1 Like

Does C++ round down? I keep getting 1 for my second set of intergers.

Also I was making a mistake and not including the “return” in

return (num1 + num2)/2;

and kept getting

0, 0 for my answers. What logic was this? What was the program doing? I cant figure it out.

EDIT:
I was using int average()
not double average()

silly me.

1 Like

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.

Incorrect, a void return type returns nothing. If you want to return a string, then use a std::string return type.

Am i missing something here?


its not letting me go further in the exercise.

Edit : it was a space between the average and the parentheses with the parameters. gotta say thats a pretty stupid way to not let me get through.