# Thread: Function variables

1. ## Function variables

I'm just learning C and I had a general question. Is it improper (bad form?) to use the same variable names in a calling function and a called function? Here's a quick example of what I mean:

main (calling function)
Code:
```int main (void)
{
int fahrenheitTemp = 0;
float celsiusTemp = 0.00;
float kelvinTemp = 0.00;;
int feetDistance = 0;
float metersDistance = 0.00;
int inchesDistance = 0;
int poundsWeight = 0;
float kilogramsWeight = 0.00;

// calculate conversions using formulas found on the internet
calc(fahrenheitTemp, feetDistance, poundsWeight, &celsiusTemp, &kelvinTemp, &metersDistance, &inchesDistance, &kilogramsWeight);

} //end main```
calc (called function):
Code:
```void calc(int fahrenheitTemp, int feetDistance, int poundsWeight, float* celsiusTemp, float* kelvinTemp, float* metersDistance, int* inchesDistance, float* kilogramsWeight)
{
calcTemp(fahrenheitTemp, celsiusTemp, kelvinTemp);
calcDistance(feetDistance, metersDistance, inchesDistance);
*kilogramsWeight = calcWeight(poundsWeight);
return;
}```
Example: Should fahrenheitTemp have a different name in the calc() function than it does in main()? In a way, keeping them the same helps maintain readability for me, but it's also a bit deceiving since they are really two different variables.

2. I've never heard anyone else's opinion on that, but I think it depends on the situation:

- When you're passing pointers to structure, having the same name would help you remember that you really are pointing to the same object
- Wen you're passing primitive types, having the same name could be confusing, because changes you make to one aren't reflected in the other

I sometimes do it solely because there's really only one good name for the variable, and that's really the most important part. Parameters in functions should be named according to how they're used in that function, and sometimes there's really only one good, succinct and consistent way to describe the purpose of a variable.

3. I think this is a two edged one, because i agree it is best to name the vars appropriate to their usage in the function, so i might have a function call with mouseX, mouseY as integers, then in the implementation i might rename them eg spriteX, spriteY, the one is a generic name that shows the vars to describe device position, the other is more specific to show the intended use in function to set a sprites movement or action or whatever.
i think this is an acceptable way to do things but i also think that while i was starting out this used to confuse me sometimes when reading others code, whereas if they were named the same then it was plain what is being passed around.
But then naming them the same as already mentioned may lead to confusion about the 'permanence' of the vars and changes made to them.
with pointers to structures i use the convention of same name with prefix 'p',
as an aside i have often read about naming conventions such as always putting a small 'i' before an integer name, 'p' with pointer etc, but never really seen it, is this an outdated thing??

4. Yes rogster, prefixing a variable name with information about its type is called Hungarian Notation. For the most part it is considered poor practice nowadays.

Popular pages Recent additions