FAQ: Code Challenge: C++ Functions - First Three Multiples

You are referring to this code:

#include <iostream>
#include <vector>

std::vector<int> first_three_multiples(int num) {
  std::vector<int> multiples{num, num * 2, num * 3};
  return multiples;
}

int main() {
  for (int element : first_three_multiples(8)) {
    std::cout << element << "\n";
  }
}

In the line of code you mentioned, multiples isn’t a function. It is neither a function declaration nor a call to a function. Instead two things are being done in a single statement. A variable is being declared AND it is being initialized in a single statement.
Suppose you want to assign an integer to some variable. You can do so in two steps or you could do it in a single step.

// In two steps
int i; // Declaring a variable
i = 2; // Assigning a value to the variable

// In a single step
int i = 2; // Declaring and initializing done in one step

The same thing is being done in the line of code you mentioned. multiples isn’t a function. It is a variable which is meant to store a vector of integers.
std::vector<int> multiples{num, num * 2, num * 3}; is a way of declaring and initializing the variable.
An equivalent alternative to do the same is std::vector<int> multiples = {num, num * 2, num * 3};
num is the parameter of the function first_three_multiples. From our main function, we call the function with an argument such as first_three_multiples(8). The argument 8 gets assigned to the parameter num of the first_three_multiples function. Within our function, we declare a variable called multiples and also initialize the vector with three elements (in a single step).

In our main function, we make the call first_three_multiples(8). The function returns a vector consisting of three elements.
If we try to print the vector directly e.g. std::cout << first_three_multiples(8); we will get an error.
The loop in main is meant to iterate over the vector and print each element of the vector on a separate line.

if the loop is meant to print contents of vector multiples, shouldn’t the statement be:

cout << element[i]

instead?

for how does the for loop work here? on the first pass, multiples already have all three multiples i.,e 8, 16, 24 in one pass!

and how the var element gets assigned? why is it not an array or vector?

Let me edit the Codecademy solution so that we can see two approaches side by side.

#include <iostream>
#include <vector>

std::vector<int> first_three_multiples(int num) {
  std::vector<int> multiples{num, num * 2, num * 3};
  return multiples;
}

int main() {
    std::vector<int> result = first_three_multiples(8);
    // Looping through a vector via its index
    for (int i = 0; i < result.size(); i++) {
        std::cout << result[i] << "\n";
    }
    // Looping through a vector via a range based loop 
    for (int element : result) {
        std::cout << element << "\n";
    }
}
  • From our main function, we make a call to the first _three_multiples functions with an argument:
    first_three_multiples(8);

  • The argument 8 is assigned to the parameter num of our function. Within our function, we declare a new vector of integers called multiples. In the same statement, we also initialize the vector with three values:
    std::vector<int> multiples{num, num * 2, num * 3};
    So, at this stage, multiples is a vector containing the values {8, 16, 24}. We need to send this vector back to our main function. We do so via the statement: return multiples;

  • In the edited main function, I have used the statement:
    std::vector<int> result = first_three_multiples(8);
    This statement declares a new variable called result. result is supposed to be a vector of integers. In the same statement, I have assigned whatever information first_three_multiples has returned to this result variable. So, now result is a vector of integers {8, 16, 24}

  • If I try to print out the result vector via std::cout << result;, the C++ compiler will throw an error because it doesn’t know how to print out a vector. So, I have used two loops. Both loops do the same thing, but the first one should be familiar to you.

  • In the first loop, I have used i as a loop variable and then I iterate over the indices of the vector. So, the first time through the loop. i will be 0 and the std::cout statement will print out result[0] which is 8. Second time through the loop. i will be 1 and the std::cout statement will print out result[1] which is 16. Third time through the loop. i will be 2 and the std::cout statement will print out result[2] which is 24. Then i will be 3 and the loop condition will be false and we will exit the loop. Codecademy has taught us this loop in earlier lessons, so this should be familiar to you.

  • Another way of doing the same as above is via a Range-based for loop. We weren’t taught this earlier in the course, but this is just a more readable and concise way of doing the same as above without bothering about the index or keeping track of the size of the vector. We loop through our vector via the statement for (int element : result). Since the values present in our result vector are integers, so we declare our loop variable to be an int. We decide to call our loop variable element (we can use a different name if we want). Now, we don’t need to bother about the index or size of the vector. The range-based for loop will take care of these details for us. The loop will go through the vector and each time through the loop, the loop variable element will hold a different value.
    So, first time through the loop, element will be 8 and we can print it via std::cout << element << "\n";
    Second time through the loop, element will be 16 and we can print it via std::cout << element << "\n";
    Third time through the loop, element will be 24 and we can print it via std::cout << element << "\n";
    Since there are no other values in our vector, we will exit the loop.

1 Like

precisely! up to this point i already learned about index based for-loops. now everything is very clear.

you explain things clearly and to the point. are you one of the instructors?

thanks for your time.

No, not an instructor. Just a learner.

Here are my codes:

#include <iostream>
#include <vector>

// Define first_three_multiples() here:
std::vector<int> first_three_multiples (int num) {
  
  std::vector<int> group;
  
  for (int i = 1; i < 4; i++) {
    group.push_back(num * i);
  }
  
  return group;

}

int main() {
  
  for (int element : first_three_multiples(8)) {
    std::cout << element << "\n";
  }
  
}

The codes would print out:

8
16
24

Why can’t I pass the challenge? It asked me “Is first_three_multiples() defined?”

All right, I know why. I had an extra space between my function name first_three_multiples and its parameter (int num).

Your standard is so rigid. Then why did you say “We don’t care what goes on in the function as long as it works the way it should. (Notice those tests in main() for each function.)” in the initial instruction?