A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
crossproductVector takes its parameters by value:
void crossproductVector(vector<float> argvec2,
vector<float> argvec3,
vector<float> argvec4)
Each argvec* is a copy of the vector passed at the call site. Inside the function, this line:
argvec4[0] = out5[0];
argvec4[1] = out5[1];
argvec4[2] = out5[2];
modifies only the local copy argvec4. The original Vec3 in the caller is unchanged, so it still has zeros after the call.
To have the function write the result back into the caller’s vector, use references (or pointers) instead of pass‑by‑value. For example:
void crossproductVector(const vector<float>& argvec2,
const vector<float>& argvec3,
vector<float>& argvec4) // note '&'
{
vector<float> out3(3, 0.0f);
vector<float> out4(3, 0.0f);
vector<float> out5(3, 0.0f);
int I = 0;
do
{
out3[I] = argvec2[I];
out4[I] = argvec3[I];
++I;
} while (I <= 2);
out5[0] = out3[1] * out4[2] - out3[2] * out4[1];
out5[1] = out3[2] * out4[0] - out3[0] * out4[2];
out5[2] = out3[0] * out4[1] - out3[1] * out4[0];
// write back into caller's Vec3
argvec4[0] = out5[0];
argvec4[1] = out5[1];
argvec4[2] = out5[2];
}
Call it as before:
vector<float> Vec1(3), Vec2(3), Vec3(3);
// fill Vec1, Vec2
crossproductVector(Vec1, Vec2, Vec3); // Vec3 now contains the cross product
Alternatively, return the result instead of trying to modify an argument:
vector<float> crossproductVector(const vector<float>& a,
const vector<float>& b)
{
vector<float> r(3);
r[0] = a[1] * b[2] - a[2] * b[1];
r[1] = a[2] * b[0] - a[0] * b[2];
r[2] = a[0] * b[1] - a[1] * b[0];
return r;
}
vector<float> Vec3 = crossproductVector(Vec1, Vec2);
The same rule explains the earlier scalar example: function parameters are passed by value unless explicitly declared as references or pointers, and a function can return only a single value of its declared return type.
References: