FAQ: Functions - Return Types — Beyond the Void

This community-built FAQ covers the “Return Types — Beyond the Void” exercise from the lesson “Functions”.

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

Learn C++

FAQs on the exercise Return Types — Beyond the Void

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!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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!

Whether I type ‘true’ or ‘false’ my programs prints 0. What have I missed?

std::cin >> support;
return support;

Probably a bit late, but this is what i found out.

It responds to both 1 [one] or 0 [zero].
For some reason it does not respond to ‘true’ and ‘false’.

1 [one] represent ‘true’ and 0 [zero] represent ‘false’

and by including std::boolalpha in the/a std::cout (meaning you could have std::boolalpha in a seperate std::cout before the actual bool is printed.) you change the output from integers to either ‘true’ or ‘false’. examples:

std::cout << std::boolalpha << needs_it_support() << “\n”;

Hope this is somewhat helpfull.

How was I suppose I had to change the
std::cout <<support:
to
return support;
This was not what the part one was asking.

Often the answer does not match at all what was ask, English is not my native language but I believe the questions are not well formulated

i keep figuring out answers on my own and by ignoring what the question is asking and peeking at the answer and understand from there.

1 Like

Nothing happens when I click the RUN button. Here is my code:

#include

// Change needs_it_support so that it returns support:
bool needs_it_support() {

bool support;

std::cout << “Hello. IT. Have you tried turning it off and on again? Enter ‘true’ for yes, ‘false’ for no.\n”;
std::cin >> support;
return support;

}

int main() {

// Change the following line to print the function result:
std::cout << needs_it_support();

}

It seems that clicking ‘Run’ in this particular exercise doesn’t compile and execute like it did in the previous exercises in the lesson. You can compile and execute the code yourself in the terminal if you like, but the outcome won’t be what is expected. support is a bool variable. When you std::cout << a bool, the output is either a 0 for false or a 1 for true. This particular needs_it_support() function will always return 0 unless the user ignores the instructions, and enters 1. The reason for that may not make sense at this point in the course.
Hope this helps, and happy coding!

Hi! Why are std::string , and std::vector used with std:: in front, while
double , int , bool and char aren’t?

1 Like

It would take some research to find out why certain data types, built-in methods, etc. are in each particular namespace, but the short answer to your question is that string and vector are not fundamental data types in C++. They are defined in the standard namespace referred to in code as namespace std, so to use them, you have to tell the compiler where to find the definition. Later you’ll start using something called using statements. For example: ( using namespace std; ). This will give you access to all of the items in the standard namespace without having to type std:: before everything belonging to the standard namespace. Hope this helps!

2 Likes

Why does the function work when we assign an integer to a ‘bool’ variable?

1 Like

C++ stores bool values as either 1 or 0. 1 is true. 0 is false. When you make an assignment to a bool variable, you can use: true, false, 1 or 0. These are considered bool values. You cannot assign just any integer to a bool variable. You can assign the bool values 0 or 1. Hope this helps!

2 Likes

Have you figured out why the program doesn’t work when we use true and false

It should work with true or false. Making assignments in code, and getting user input are a little different. Printing the output to the console as ‘true’ or ‘false’ also requires more than is taught at this point in the lessons.
This example will take 1 or 0 as input, but output true or false:

#include <iostream>

int main() {
  bool answer;
  std::cout << "Enter 1 for true or 0 for false:\n";
  std::cout << "The sky is blue.\n";
  std::cin >> answer;
  std::cout << "\nYou answered: " << std::boolalpha << answer;
  return 0;
}

Output:

Enter 1 for true or 0 for false:
The sky is blue.
1
You answered: true

This example will take true or false as input, but output 1 or 0:

#include <iostream>

int main() {
  bool answer;
  std::cout << "Enter true or false:\n";
  std::cout << "The sky is blue.\n";
  std::cin >> std::boolalpha >> answer;
  std::cout << "\nYou answered: " << answer <<"\n";
  return 0;
}

Output:
Enter true or false:
The sky is blue.
true
You answered: 1

The std::boolalpha allows the input to be true or false in a std::cin statement or changes the output to true or false in a std::cout statement. If our code didn’t use input from the user, we could assign any of the accepted bool values to the bool variable in our code:

 bool mybool;
 mybool = 1;
 mybool = 0;
 mybool = true;
 mybool = false;

Hope this helps.

2 Likes

I don’t really understand this website so I don’t know if I am asking my own question or replying to somebody elses, but I don’t understand when to simply call the function like:
function(); or if you are supposed to: std::cout << function();. Could someone shed some light on when to use which.

1 Like

The short and simple answer is that you would only use std::cout << some_function(); if you want to print the value returned by the function to the console. If you just want the function to execute without printing the result invoke it with some_function();.

2 Likes

I am confused by this exercise, perhaps because I am new to coding. If I retype the entire solution - which I’ve done a few times now - , I still get this error: image

Why?