"Pass vectors from one function to another function" is conceptually off the mark. We do pass data between
calling function and
called function, but that is not the pair of functions that you were thinking of.
1 2 3 4 5 6
|
int main() {
constexpr int N {42};
auto foo = set_matrix_coef( N );
auto bar = set_rhs( N );
auto gaz = gauss_seidel( foo, bar );
}
| |
Function main() passes N to function set_matrix_coef() and receives a vector that it stores in object foo.
Function main() passes N to function set_rhs() and receives a vector that it stores in object bar.
Function main() passes foo and bar to function gauss_seidel() and receives a vector that it stores in object gaz.
Caller can pass data to called function by value or by reference.
Caller can receive data from called function as return value or by reference.
A function can access data that is in the same scope as the function. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
struct Solver {
Solver( int size ) : N(size) {}
void set_matrix_coef();
void set_rhs();
vector<double> gauss_seidel();
private:
const int N;
vector<double> b;
vector< vector<double> > mat_coef;
};
int main() {
Solver magic( 42 );
magic.set_matrix_coef();
magic.set_rhs();
auto gaz = magic.gauss_seidel();
}
| |
The member functions of the struct can access the member variables of the struct.
We don't usually say that we would "
pass data from magic.set_rhs() to magic.gauss_seidel()".
No, we might say that both magic.set_rhs() and magic.gauss_seidel() access/modify the attributes of magic.