Codecademy Forums

FAQ: Functions - Tackling Multiple Arguments

This community-built FAQ covers the “Tackling Multiple Arguments” exercise from the lesson “Functions”.

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

Learn C++

FAQs on the exercise Tackling Multiple Arguments

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!

I have made it to step 4 and I have to call name_x_times with arguments my_name and some_number within int main(). My problem is that when I type in the code it says that I need to type it within main() although I already am. What am I doing wrong? If somebody could please help that would be greatly appreciated.
#include

// Define name_x_times() below:
void name_x_times(string name, int x)
{
while (x>0)
{
std::cout<< name;
x–;
}
}

int main() {

std::string my_name = “Add your name here!”;
int some_number = 5; // Change this if you like!
// Call name_x_times() below with my_name and some_number

name_x_times(my_name, some_number);
}

Your subtraction operation doesn’t look correct. Try to add another minus sign:

x - -;

Thanks for the suggestion. Although I do already have two minus signs, it just does not appear that way in the text above. Any other suggestions?

Thank you for your help chipcoder46164, my problem with the code was that I needed to put (std::string name, int x).

1 Like

just finished step 4 in this chapter

¨my code.

#include <iostream>

// Define name_x_times() below:
void name_x_times(std::string name, int x) {
  while (x > 0); { 
  std::cout << name;
  x--;
    
  } 
    
}

int main() {
  
  std::string my_name = "Jonathan";
  int some_number = 5; // Change this if you like!
  // Call name_x_times() below with my_name and some_number
  name_x_times (my_name, some_number);  

}

and cant move on the circle when pressing run is just rolling around,

thanks in advanced,

zyph

1 Like

Question: how come strings have to be defined with std::string x; , but ints can just say int x; without the std:: thing?? Which vars have to be defined before you give the type and which don’t??

Thanks,
Dawn

#include <iostream>

// Define name_x_times() below:
void name_x_times(std::string name, int x) {
  
while (x > 0) {
  
std::cout << name << "\n";
x == x-1;  
  
  
}
  
}


int main() {
  
  std::string my_name = "Add your name here!";
  int some_number = 5; // Change this if you like!
  // Call name_x_times() below with my_name and some_number
  
  
}

I have cleared steps 1 and 2. In step 3 I was told to print the string variable ‘name’ and then decrease the variable ‘x’ by 1. I wrote ‘x = x-1;’ and clicked the ‘Run’ button. It doesn’t give the correct result. But when I wrote x–; and clicked the ‘Run’ button, it gave the correct result. I remember that I used this type of syntax and got the correct answer in some other exercises and got the correct answer. Is the syntax ‘x = x-1;’ incorrect? (or) Is any other mistake present in my program?

I don’t understand why this code needs to have a while loop. And I especially don’t understand why the parameter inside the while loop says x > 0 when the loop keeps subtracting 1 from x. Why isn’t it x < 0?

Here’s my code:

#include <iostream>

void name_x_times(std::string name, int x) {
  while (x > 0) {
    std::cout << name;
    x--; 
  }
}

int main() {
  
  std::string my_name = "Riya. ";
  int some_number = 5; 

  name_x_times (my_name, some_number);
}

Any help would be greatly appreciated! :slight_smile:

The code doesn’t “need” a while loop per se’, but this is one way to repeat a block of code a specific number of times. Your function name_x_times() takes 2 parameters. The first is the name to print to the console, and the second is the number of times to print it. The variable name will be assigned the name, and x will be assigned the number. while will repeat the code inside it’s code block { } as long as the condition in the parenthesis evaluates to true. In your main() function you are calling the name_x_times() function with the arguments: "Riya. " and the number 5, so the first time this line is executed: while (x > 0) x is 5. std::cout << name; prints Riya. to the console, and x is decremented by 1. while (x > 0) is evaluated again, and since x is now 4 Riya. is printed a second time. This process continues until x is no longer greater than 0 resulting in this output:

Riya. Riya. Riya. Riya. Riya.

Hope this helps!

This makes much more sense, thank you so much for your help!!

1 Like

Is anyone else also experiencing bugs where you write valid code (even the same as the official solution) and it isn’t being accepted? For this exercise’s final piece I called the function as below

name_x_times(my_name, some_number);

and it wasn’t accepted. It was in the right place and everything. I’m a free member, but I won’t be coughing up the subscription price anytime soon considering how many bugs like this I’ve run into.

I’m having an issue just with the first bit defining the function- for some reason the underscore in the name of the function make it stop working properly.

!
When i write it properly name_x_times it suddenly goes red and wont be recognised as a function. Does anyone know what is doing this and how to fix it