FAQ: Functions - Review of C++ Functions

This community-built FAQ covers the “Review of C++ Functions” exercise from the lesson “Functions”.

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

Learn C++

FAQs on the exercise Review of C++ Functions

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!

Mine won’t execute

it.cpp

#include <iostream>
#include "functions.hpp"
int main() {
  
  // Conduct IT support
  // Check in with Jenn

  // Conduct IT support again...
  // Check in with Roy
  // Conduct IT support yet again...zzzz...
  
}

header file functions.hpp

std::string hello_it(std::string on_off_attempt);

std::string hi_jen();

std::string hello_it(std::string on_off_attempt);

std::string hi_roy();

std::string hello_it(std::string on_off_atempt);

definitions file functions.cpp


std::string hello_it(std::string on_off_attempt){
  std::cout << "Hello. IT.\n";
  std::cout << "Have you tried turning it off and on again? y/n\n";
  std::cin >> on_off_attempt;
}

std::string hi_jen()
{
   std::cout << "Oh hi Jen!\n";
}

std::string hi_roy();
{
  std::cout << "You stole the stress machine? But that's stealing!\n";
}

Ok, I got it to work like this, any ideas in how to further refactor ?

#include
std::string it_crowd(int tries){

std::string yes_or_no;
std::string attempts = “Hello IT. \n”“Have you tried turning off and on again? y/n\n”;
int x = 0;
while (tries > 0) {
std::cout << attempts;
std::cin >> yes_or_no;
x++;
tries–;
if (x == 1) {
std::cout << “Hello Jen!\n”;
}
else if (x == 2) {
std::cout << “You stole the stress machine? But that’s stealing!\n”;
}
}
return attempts;
}

int main() {
it_crowd(4);
}

1 Like

I have yet to learn what this “.hpp” is but i think one of the issues is here :

std::cout << “Have you tried turning it off and on again? y/n\n”;
std::cin >> on_off_attempt;

It appears that on the original code provided the “on_off_attempt” doesn’t really return any meaningful data to the function, it’s just there to bring up the prompt after “Have you tried turning it off and on again? y/n\n”. In your code it appears that whatever you input will go to the only parameter in “hello_it()” I would think that would cause some issues.

The parameter from what I understand is just a variable, which you input data when you call the function so if you are calling “hello_it()” here on the second block :

std::string hello_it(std::string on_off_attempt);

std::string hi_jen();

std::string hello_it(std::string on_off_attempt);

std::string hi_roy();

std::string hello_it(std::string on_off_attempt);

Your are inputting " std::string on_off_attempt " into " std::sin >> on_off_attempt"

Probably poorly explained if its even correct, maybe someone that knows better will post :smile:

how do i pass the and of the functions chapter c++?? please help

1 Like

Hello @alex1010110. Welcome to the forum.

You’ll have to post a specific question, or share the code your working on that you’re having trouble with along with a question. A link to the exercise would also be helpful.

While the program does not do anything with std::cin >> on_off_attempt;, trie to make it a void function:

void hello_it () {
std::cout << “Hello. IT.\n”;
std::cout << “Have you tried turning it off and on again? y/n\n”;
std::cin >> on_off_attempt;
}

I am just beginning in coding, so I do not know if this is correct (tell me if not), but it is working.

1 Like

Does codecademy provide an actual answer for this? It would be nice to see how they did it. This is what I got and I don’t know if it is right. Would someone tell me what I am missing? It compiled but after answering the first y/no question it gave me an error: image Thank you.

#include

string it_support() {
std::string on_off_attempt;
std::cout << “Hello. IT.\n”;
std::cout << “Have you tried turning it off and on again? y/n\n”;
std::cin >> on_off_attempt;
}

int main() {

// Conduct IT support
it_support();

// Check in with Jenn
std::cout << “Oh hi Jen!\n”;

// Conduct IT support again…
it_support();

// Check in with Roy
std::cout << “You stole the stress machine? But that’s stealing!\n”;

// Conduct IT support yet again…zzzz…
it_support();

}

I execute this program and it ran, just when I type answering “Have you tried turning it off and on again? y/n\n” it duplicates my answer(yes or no) in the next line , I wonder why?

std::string on_off_attempt() {
std::string a ;
std::cout << “Hello. IT.\n”;
std::cout << “Have you tried turning it off and on again? y/n\n”;
std::cin >> a;
return a;
}
int main() {

// Conduct IT support
std::cout << on_off_attempt() << “\n”;

// Check in with Jenn
std::cout << “Oh hi Jen!\n”;

// Conduct IT support again…
std::cout << on_off_attempt() << “\n”;
// Check in with Roy
std::cout << “You stole the stress machine? But that’s stealing!\n”;

// Conduct IT support yet again…zzzz…
std::cout << on_off_attempt() << “\n”;

}
Thanks.

yeah uh so i don’t understand what are we supposed to code here. Like the goal of that program and what it’s supposed to do

2 Likes

Same… I think the ‘IT’ example for the exercise is dumb… while reading it I didn’t understand what was even going on.

This is what I did… but I simply don’t understand what the on_off_attempt was supposed to trigger…

#include <iostream>
  // Variable
  char on_off_attempt;
  

  // Functions
  void default_response() {
    std::cout << "Hello. IT.\n";
    std::cout << "Have you tried turning it off and on again? y/n\n";
    std::cin >> on_off_attempt;
  }

  void contact_it(bool check_in, int check_in_who) {
    // Conduct IT support
    default_response();

    // Check in with ... (for whatever ■■■■ reason... seems so unnecessary and annoying)
    if (check_in == true && check_in_who == 1) {
      std::cout << "Oh hi Jen!\n";
    } else if (check_in_who == 2) {
      std::cout << "You stole the stress machine? But that's stealing!\n";
    }

  }

int main() {

  // Run Program
  contact_it(true, 1);
  contact_it(true, 2);
  contact_it(false, 0);
  
}

this is what i did. please can someone check if i am right or not. thanks

#include

bool needs_it_support() {

bool on_off_attempt;

// std::string on_off_attempt;

std::cout << “Hello. IT.\n”;

std::cout << “Have you tried turning it off and on again? y/n\n”;

std::cin >> on_off_attempt;

return on_off_attempt;

}

int main() {

// Conduct IT support

/* std::string on_off_attempt;

std::cout << “Hello. IT.\n”;

std::cout << “Have you tried turning it off and on again? y/n\n”;

std::cin >> on_off_attempt;

*/

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

// Check in with Jenn

std::cout << “Oh hi Jen!\n”;

// Conduct IT support again…

/* std::cout << “Hello. IT.\n”;

std::cout << “Have you tried turning it off and on again? y/n\n”;

std::cin >> on_off_attempt;

*/

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

// Check in with Roy

std::cout << “You stole the stress machine? But that’s stealing!\n”;

// Conduct IT support yet again…zzzz…

/* std::cout << “Hello. IT.\n”;

std::cout << “Have you tried turning it off and on again? y/n\n”;

std::cin >> on_off_attempt;

*/

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

}

this is how i changed the code they provided to this ans it works. please if it is not correct then i need an explanation. thanks

include

bool needs_it_support() {

bool on_off_attempt;

// std::string on_off_attempt;

std::cout << “Hello. IT.\n”;

std::cout << “Have you tried turning it off and on again? y/n\n”;

std::cin >> on_off_attempt;

return on_off_attempt;

}

int main() {

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

// Check in with Jenn
std::cout << “Oh hi Jen!\n”;

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

// Check in with Roy

std::cout << “You stole the stress machine? But that’s stealing!\n”;

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

}

This works fine for me.

#include
#include <string.h>

std::string it() {
std::string on_off_attempt;
std::cout << “Hello. IT.\n”;
std::cout << “Have you tried turning it off and on again? y/n\n”;
std::cin >> on_off_attempt;
return on_off_attempt;
}

int main() {

// Conduct IT support
it();

// Check in with Jenn
std::cout << “Oh hi Jen!\n”;

// Conduct IT support again…
it();

// Check in with Roy
std::cout << “You stole the stress machine? But that’s stealing!\n”;

// Conduct IT support yet again…zzzz…
it();

}

1 Like

So in a practical sense, void functions return every print statement?

No, void functions don’t return anything. Printing a statement to the terminal and returning something are not the same thing.
Suppose we have a function. If we want to send some information from this function back to our main program (or some other function), then we must use a return statement within our function (and the return type will depend on what information we want to send back). Within our function, we can print things to the terminal and/or do calculations and/or call other functions but our final job within the function will be to return the information we promised.
If we don’t want to send information back, then we use a void function i.e. a function which doesn’t return anything. Within our void function, perhaps we may print something to the terminal. But, it is also possible that our function doesn’t include any print statements. Perhaps our function makes a call to another function. Perhaps it does something else which doesn’t involve any print statements.
A function, regardless of whether it is a void function or whether it returns something, can have multiple print statements. But printing something to the terminal is not the same as passing information back to the main program. A void function returns nothing.

3 Likes

Thank you for the clarification.
Non-void functions return a certain type of data to either the main function or another function, if I understood your explanation correctly.

1 Like

Yes. If the main function makes the call to the the non-void function, then the non-void function will return some data to the main function.
If instead of the main function, another function makes the call to the non-void function, then the non-void function will return the data to that function instead of main.

3 Likes

I finally figured out how to make this work properly. If you want it to loop and you want code as reference, look at this code. It took me a long time to figure it out.

#include <iostream>

// Contact IT department again
// In c++ this function goes above contactIT
// because it is being called in that function
void contactAgain(bool& running) {
  std::string on_off_attempt;
  std::cout << "Hello. IT.\n";
  std::cout << "Have you tried turning it off and on again? y/n\n";
  std::cin >> on_off_attempt;

// Takes user input
    if (on_off_attempt == "y") {
      std::cout << "You stole the stress machine? But that's stealing!\n";
    } else {
      running = false;
    }
}

// Contacts IT the first time
void contactIT(bool& running) {
  std::string on_off_attempt;
  std::cout << "Hello. IT.\n";
  std::cout << "Have you tried turning it off and on again? y/n\n";
  std::cin >> on_off_attempt;

    if (on_off_attempt == "y") {
      std::cout << "Oh hi Jen!\n";
  contactAgain(running);
    } else {
      running = false;
    }
}

int main() {
  // Define bool function so we can 
  // loop this program
  bool running = true;

  // While running ^ is true, run contactIT
  while (running == true) {
    contactIT(running);
  }
}