0

When passing variables by reference, should you keep the same variable name in the function? E.g.

int main()
{ 
const int Players = 1000;
array <double, Players> PlayerStrengths;
}

template <typename T, size_t Size>
void GeneratePlayerStrengths(array <T, Size>& _PlayerStrengths) 
{
//-- Logic here --
}
whitebloodcell
  • 308
  • 1
  • 4
  • 10
  • 1
    What has this to do with references? And your code has undefined behaviour, so don't do that. – Kerrek SB Apr 09 '14 at 07:28
  • 1
    Makes absolutely no difference; personal preference. – OMGtechy Apr 09 '14 at 07:28
  • see f.e. http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier for the undefined behavior – wonko realtime Apr 09 '14 at 07:36
  • I thought preceding the name of the variable (in this case _PlayerStrengths) with the reference operator (&) meant I was passing by reference? Am I getting the terminology wrong here? The code seems to work with the underscore, but I will change it if this is not something you should normally do. – whitebloodcell Apr 09 '14 at 08:52
  • That is correct, it is being passed by reference. The issue is that, technically, names prefixed with underscore are reserved. Just drop the _, and leave your parameter name as PlayerStrengths. – mjs Apr 09 '14 at 09:48

3 Answers3

2

No. They may have different names.

Also, if you start a name with an underscore followed by an uppercase letter, the behavior is undefined. Don't do that.

2

When passing variables by reference, should you keep the same variable name in the function?

In the general case no. Or at least there is no reason why they should.

In fact I would suggest you might want to approach the issue from the other way, and say

"should the calling function use variable names that are the same as the parameter name in functions it is calling?"

In this case the answer is maybe

For instance, if an api to send a message takes a parameter named msg, you might want your calling function to use the same variable name

However, the important rule is that the variable name inside the function you are calling should make sense in the context of that function, and the variable name in the calling function should make sense in that context.

For instance:

int SendMsg( Msg& msgToSend)
{
// Serialise and transmit message

  return OK;
}


void GenerateAndSendShutdown (void)
{
   //You could call this msgToSend, but it wouldnt make the code any clearer
   Msg shutdownCmd = "Shutdown";
   SendMsg (shutdownCmd);
}

In your particular case, I would say it would make sense if they were the same.

mjs
  • 2,837
  • 4
  • 28
  • 48
-1

Variable passed are of two types: formal arguments (as seen as function arguments) e.g. void func(int& formalarg) and actual arguments (as arguments passed while calling a function), e.g. func(actualarg), where actualarg may or maynot be of integer type. In case the typese differ, there has to be cast operator available from other type of actual argument to int (expected formal argument type). If two types are same, for program readability keeping the same name makes the program more elegant to be understood about the purpose provided you follow a meaningful naming convention. Hope this helps.

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69