Reputation: 272
Are there any rules (or will I run into any problems) if I name the parameters of a function the same as the variable I will pass into them? For example in Python:
def foo(param):
pass
param = 2
foo(param)
In the fairly limited programming I've done, I have not ran into any problems doing this. Will I get problems in certain languages? Is this okay to do, or is it a practice to be avoided?
Upvotes: 0
Views: 72
Reputation: 522321
The "problem" in this particular case is that the function parameter name will shadow the outer param
variable; i.e. you cannot (implicitly) refer to the global param
anymore because inside your function param
is defined as a local variable.
But, this is really as it should be. Your function should only worry about the parameters it declares locally, not about implicit global variables. Conversely, a caller of a function should not have to worry about anything that goes on inside a function. Naming a variable the same as a parameter to a function is of no consequence to the caller, and should be of no consequence to the function itself.
So, no, there's absolutely no issue here.
Upvotes: 2