a curious question about variables in functions

Hi everyone,

I have a question about variable declaration in the parameter of functions. I have noticed that in many codes of experienced programmers, the name of the variable they declare in the parameter of their function is different from the name of the variables they declare in int main().

To make this more clear, I will provide an example:

1
2
3
   int main()
{
    char grade;


and then the beginning of the function:

int qualityPoints(char letterGrade) {

My question is, since grade will be evaluated by the function quality points, why not name the variable in the parameter grade as well?

I am hypothesizing that it has something to do with calling on that function multiple times for different variables, but as a new programmer I'm not sure.

Can somebody clear this up for me?

Thanks a lot in advance!
you can put same name. it's not prohibited.
Thanks for your reply. Maybe I should make my question more clear, sorry!

I know you are able to put the same name, but I'm wondering why most experienced programmers use different names. Isn't it easier to follow how information is processed when you use the same names?
int main()
{
sum(i,j);
......
.......
}
int sum(int g,int h)
{
return (g+h);
}

here g and h are the copy of the variables i and j respectively;htese are known as formal parameters......
theymay have same or different name
Thanks for your reply, I will just assume that it is just a style choice.

It is not a style choice.

The variable name given in the function should make sense IN THE FUNCTION. Here is an example function:

1
2
3
4
int addTwoNumbers (int firstNumberToAdd, int SecondNumberToAdd)
{
  return (firstNumberToAdd + SecondNumberToAdd);
}

This is a very simple function for example purposes. You can look at it and just be reading the first line, understand what the input parameters are for. It is easy to understand, because I chose names for the parameters that make it clear what they do in the function.

Now, I have the function. I am writing a program that involves, for example, cats in boxes. I have variables:

1
2
int catsInBoxOne;
int catsInBoxTwo;

This code is easy to understand. Each variable is the number of cats in a box.

Later, I need to know how many cats I have in total, so I use my function:

int numberOfCats = addTwoNumbers(catsInBoxOne, catsInBoxTwo);
This code is easy to understand. It is adding catsInBoxOne and catsInBoxTwo. Everything is easy to understand because I picked sensible variable names.

Are you seriously suggesting that I rename my variables catsInBoxOne and catsInBoxTwo to firstNumberToAdd and SecondNumberToAdd? That would make the code harder to understand, and would create the impression that these variables are just numbers I want to add, instead of what they actually are. If I ever did anything other than adding them together, would be confusing; what happens if I have a different function for doing something else:

1
2
3
4
int subtractTwoNumber(int numberToSubtractFrom, int numberToSubtract)
{
  return (numberToSubtractFrom - numberToSubtract);
}

and I am going to apply this function as well to my variables catsInBoxTwo and catsInBoxOne. Now what do I name them? I'm going to use both functions. Should I rename my variables (according to your naming scheme where variable names match function parameters) numberToSubtractFrom and numberToSubtract, or firstNumberToAdd and secondNumberToAdd?

Functions can be used many times. That's why we have them. If I made my variables match the names used in the function, how could I ever use the function repeatedly?

1
2
3
4
int catsInBoxOnefirstNumberToAdd;
int catsInBoxTwosecondNumberToAdd;
int catsInBoxThree;
int catsInBoxFour;


What do I rename catsInBoxThree and catsInBoxFour? I can't rename them firstNumberToAdd and secondNumberToAdd, because those names are already being used.

Functions exist to make my life easier. If I write a function that I use again and again, I do not want a function that I might have written years ago to force me to make my code harder to understand. That's insane.

I will just assume that it is just a style choice.

It is NOT a style choice. It is NOT a style choice.

It is a fundamental part of the reason we have functions and to misunderstand that is to misunderstand why we have functions. Right now, you're missing the point.

Last edited on
I know you are able to put the same name, but I'm wondering why most experienced programmers use different names. Isn't it easier to follow how information is processed when you use the same names?
It's about context.

Functions exists in a general context hence the parameter do have general names.

When you call the function it is a specific context and you can have more specific names. Therefore the names sometimes differ.

Wow! Thank you so much for your detailed response. I completely understand what you mean and I can see how it is important to name variables in functions in such a way that they make sense in each individual function.

Until now I was completely missing the point! Many of the examples I was learning from had such similar named variables between int main() and the functions, but they were slightly different like "num1" and "number1". I think that was confusing me a little bit, but I totally understand now thanks to your well thought out reply!

Crucial thanks Moschops!

Thanks everybody!
Last edited on
Topic archived. No new replies allowed.