Fundamentally confused about functions and return

Hello everyone,

I’m doing the challenge activities for the chapter on functions. The chapter before it was on vectors, and the task is as follows: Call a function (first_three_multiples) which takes an integer, and then returns a vector with the first 3 multiples of that integer. For example, first_three_multiples(5) should yield a vector with 5, 10, and 15.

#include
#include

// Define first_three_multiples() here:
std::vector first_three_multiples(int num){

std::vector numv(3);
numv[0]=num;
numv[1]=2num;
numv[2]=3
num;

return numv;
};

int main() {
first_three_multiples(8);

for(int i=0; i < first_three_multiples;i++){
std::cout << first_three_multiples[i];
}

}

This is my code so far. I’m receiving an error message about the for loop at the end: “main.cpp: In function ‘int main()’:
main.cpp:20:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
for(int i=0; i < first_three_multiples;i++){
^~~~~~~~~~~~~~~~~~~~~
main.cpp:21:41: warning: pointer to a function used in arithmetic [-Wpointer-arith]
std::cout << first_three_multiples[i];”

Apparently I violated some rule, what did i do?
^

Wel worth having a look at: How do I format code in my posts? which would let you keep the content as code (no forum formatting removing half the content).

That error is quite verbose but if you read through it carefully it is fairly good at pointing out the issue “warning: pointer to a function used in arithmetic”. Be careful with the identifiers you’re passing around, what do i and first_three_multiples actually refer to here?

Just take a moment and consider exactly what you want i to be compared against.

If you’re returning something from a function consider where you’re going to store this data, using the return without storing it away can be very difficult unless you’re very careful about object lifetimes.

2 Likes