Grinding to a complete halt!

Hi there my coding genius comrades. I've been learning coding for the first time in my life for the past 2 weeks. got to chapter 9 (Vector's). Understanding the basic principle of vectors. Pushing back, popping back etc. But then this thing below comes up, and just throws 3 sharks in the water.

I need some VERY basic level help with explaining what the hell is happening in bold. The guy says let's determine our functions return type, and then goes on. That top line before the open { just melted me.

i can't work out what int sumVector (vector<int> vect)does, on any level. we've got int sumVector, we've got vector<int> AND we've got vect. All on one line, apparently 3 seperate things, tyying to do 1 thing. Get a total.

Can anyone shed any light for me? it's been 2 days of stuck..ness.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <vector>

using namespace std;

int sumVector (vector<int> vect)  {
    int total = 0;
    for (int i = 0; i < vect.size(); ++i)
        total += vect[i];
    return total;
    }
int main()
{
    vector<int> grades;
    int total;
    grades.push_back(84);
    grades.push_back(77);
    grades.push_back(92);
    grades.push_back(98);
    grades.push_back(88);
    total = sumVector(grades);
    cout << "The total is : " << total << endl;
    return 0;
}
It's a pretty basic function definition. You might want to go back and re-read the section on functions if you don't understand it.

A "function" is a block of code that can be "called" from elsewhere in the program. When you "call" a function, the program will jump to the start of it, run its contents, then jump back to the part of the code that called it.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
void somefunction() // <- a function
{
    cout << "example" << endl;  // <- this is what the function does
}

int main()
{
    somefunction();  // <- calling the function.  This line will jump to the body
       // of our 'somefunction' function.. then when that function exits (or "returns"),
       // the program will jump back here

    somefunction();  // <- call it again
}


In this example, since the function is called twice, it will have the below output:
example
example



Your example:
 
int sumVector (vector<int> vect)


This is also a function. It's different in 2 key ways:

1) It has a return type (it returns an int)
2) It takes a vector as a parameter

Return values are used for "output" of the function... ie so the function can send information back to the code that called it.

Parameters are "input" for the function... so the function can work with some data given to it by the code that called it.

In this case, the function takes a vector as input... sums all the contents... then "returns" that sum back to the calling code.
Last edited on
thats very clear. but in the case like above of int (vector<int> vect) where exactly in between these brackets is it taking the vector as an input, and summing the contents. is it like (vector<48> vect) or maybe (vector<int> 48) meaning that vect actually is storing the variables in the vector on each run through the loop, or is the <int> where the total is being stored eachtime through the loop etc?

not sure if you get what i mean. int sumVector (vector<int> vect) which parts of this line are the actual parts that will receive or take a number into them in order to sum them and send them to total.

Thanks
Okay let's back up a bit and look over a similar but simpler function:

1
2
3
4
5
6
7
8
9
10
int func( double param )
{
  //...
}

int main()
{
    int x = func(10.5);
    int y = func(11.6);
}


In this example, 'func' takes a double as a parameter and returns an int. Hopefully that shows the syntax a bit more clearly:

 
return_type  function_name(  parameter_type  parameter_name )


When we call the function, we have to give it a double to fill in that parameter, hence why there's a floating point value when we call it:

1
2
int x = func(10.5); // <- the 10.5 here is our 'double' parameter
// whatever 'func' returns gets assigned to 'x' 





So now that that's [hopefully] understood... let's go back and look at vector:

1
2
3
    vector<int> grades;
    //...
    grades.push_back(84);


A vector is basically just a resizable array... but a resizable array of whats? The type in the <angle brackets> indicates what we have a vector of. So here... vector<int> indicates we have a vector of ints.

We could have a vector<double>... or a vector<string>... or a vector<any_other_type_imaginable>. All of those vectors would be considered different types. A vector<int> is a different type from vector<double> just as 'int' is a different type from 'double'.

So vector<int> is the type. So this function:

int sumVector (vector<int> vect)

Is the same as this function:

int func( double param )

The only difference is that instead of passing a double as a parameter, we're passing a vector of ints.
thats excellent thank you mate! i kinda got it now. it's just the fact that the way the vector is written confused me at first. so basically i have it! you're a star
Topic archived. No new replies allowed.