A week ago I started studying the subject of pointers and dynamic arrays. As a practice, I have made the following program that allows the user to add two vectors of the size they want:
#include <iostream>
#include <iomanip>
using namespace std;
void leer_vector(int v[], int n)
{
cout << "Ingrese los elementos del vector:\n\n";
for (int i = 0; i < n; i++)
cin >> v[i];
}
void escribir_vector(int v[], int n)
{
cout << "Los elementos del vector son:\t\t";
for (int i = 0; i < n; i++)
cout << v[i] << setw(5);
}
void sumar_vectores(int v1[], int v2[], int v3[], int n)
{
for (int i = 0; i < n; i++)
v3[i] = v1[i] + v2[i];
}
int main()
{
int dimension_vector;
int *p_vector1, *p_vector2, *p_vector3;
cout << "\250Cu\240ntos elementos desea ingresar?:\t";
cin >> dimension_vector;
cout << '\n';
p_vector1 = new int[dimension_vector];
p_vector2 = new int[dimension_vector];
p_vector3 = new int[dimension_vector];
leer_vector(p_vector1, dimension_vector);
cout << '\n';
leer_vector(p_vector2, dimension_vector);
cout << '\n';
escribir_vector(p_vector1, dimension_vector);
cout << "\n\n";
escribir_vector(p_vector2, dimension_vector);
cout << "\n\n";
p_vector3 = sumar_vectores(p_vector1, p_vector2, dimension_vector);
escribir_vector(p_vector3, dimension_vector);
cout << "\n\n";
delete[] p_vector1;
delete[] p_vector2;
delete[] p_vector3;
system("pause");
return 0;
}
My doubt is when defining the procedure void sumar_vectores
because I have used four parameters and I am not convinced to use that number of parameters. So, on the one hand, is it correct to use more than three parameters in a function/procedure? According to this answer , it should be avoided whenever more than three parameters can be used (that's how I understand the answer).
And if that were the case, how could I modify the procedure definition void sumar_vectores
so that it uses three or fewer parameters?
Thanks in advance for answers and/or comments.
As Jack the Ripper said , let's go by parts:
Well, like almost everything... it depends .
logical factors.
In the answer you indicate, they are referring to logical problems ; that is, using too many arguments may indicate poor function design. Perhaps that function is doing too much work, and it would be wise to break it down into several smaller functions.
This is not always the case : some functions simply require a certain number of arguments to work (such as your own
sumar_vectores( )
.However, there are mechanisms to facilitate the use of such functions; as indicated in the answer itself, it is very common to use complex data types, such as
struct
(in C) orclass
(in C++).To keep things short: instead of creating a function with 5 arguments, you create a function with
struct
5class
fields , fill it, and pass it as the only argument.physical factors
This is a different point of view. On certain architectures, using fewer arguments can speed up the function call.
For example, on x86, the fastcall calling convention uses 3 registers (I think it was 3, not sure) to pass the first 3 arguments, and pushes the rest on the stack. In contrast, cdecl or pascal push all arguments on the stack, so they are slower. By the way, fastcall is used by default ;-)
Pushing onto the stack is slower than using registers, since it involves memory reads/writes.
But... these behind-the-scenes things shouldn't concern you unless you're writing critical code (speed or size), and you can ignore them 99.99% of the time. The compiler is up to something, and it will take care of optimizing the code as much as it can.
Also, C++ can use functions
inline
, which don't even involve a true call . Come on, you can ignore the physical issue ... although I think it's worth knowing, since we're dealing with the matter :-)Well, as I said before... it depends .
You're in C++... Use classes!
In your case, you could do
The above can perfectly be used for what you intend. And, by using classes, you save the last argument of your call, which would have only 3 arguments:
And, while we're at it, I challenge you to implement this:
With what you save another one more; and even, overloading the
operator+( )
:So you still use 2 arguments, but it looks prettier :-)
Speed or size?
Another way to attack your specific case is to use a special value to indicate the end of the vector ; for example, use
-1
as an indicator.So, when filling, when the user finishes typing, you manually put a
-1
in the array.Now, you can also save the last argument of your function... at the cost of having to calculate, each time , the size of the vector, counting the elements until you get to
-1
.So your function would be
Of course you could also take advantage of the function's ability to return things:
and you would be left with only 2.
Already put ...
Sticking with your specific case, you could still change the logic behind your vectors ; in your code, the size of the vector is saved in a variable for that purpose... can't you save that size in the vector itself? Specifically, in the position
vector[0]
.With this, your function remains the same as in the previous case:
But now you don't need to calculate the size on each call... instead, your actual data starts at position
vector[1]
.And the control logic of your vectors gets a bit complicated; for example, to create a 7-element array :
Now to finish and curl the curl a little more:
With this last step, your vector can be used transparently with other functions, but you know that to get the size, just do
Ummm... I think this question will end up being closed for opinion based or too broad :-O
There is nothing wrong with it, and there is nothing right with it either because it is not a correctness problem. The answer you linked (from SoftwareEngineering) talks about Uncle Bob 's Clean Code book as a reference for determining the number of parameters in a function, but there are several reasons why that answer has nothing to do with the fix:
As a curiosity, the C++ standard indicates in Annex B that the parameters that a function can receive depend on the compiler implementation, but that they should be at least 256.
If you need 256 parameters in a function it doesn't mean it's wrong 1 , but it will make the code extremely difficult to understand.
There is no need to reinvent the wheel.
The C++ Standard Library offers a wrapper that is intended precisely for operations like the ones you are doing:
std::valarray
, you can add twovalarray
in the most natural way possible, using a plus symbol:1 Surely indicates poor design decisions and/or absurd requirements.