In the code below, I do not understand the difference between the two versions. In both cases, I make a passage by reference, it seems to me (I think).
Moreover, I do not understand why version 2 works, because for what I understand for the moment, the sign & allows me to give the address of a variable, so when I put function_name(int &yourInt) I technically ask the user to enter the address of an int? So I should call it like function_name(&myInt)? But here we call the function like function_name(myInt) instead.
Version 1 :
int value(int tab[], int *valeur)
{
for (int i = 0; i < 5; i++)
{
*valeur += tab[i];
}
return *valeur;
}
int main()
{
int test = 0;
int tab[10] = { 1,2,3,4,5};
std::cout << value(tab, &test) << std::endl;
std::cout << test;
}
Version 2 :
int value(int tab[], int &valeur)
{
for (int i = 0; i < 5; i++)
{
valeur += tab[i];
}
return valeur;
}
int main()
{
int test = 0;
int tab[10] = { 1,2,3,4,5};
std::cout << value(tab, test) << std::endl;
std::cout << test;
}