c++ - Is it okay to duplicate function argument name when in implementation and in calling? -


if have

#include <iostream> int fun(int,int);  int main(void) {    int foo=3;    int bar=5;    std::cout << fun(foo,bar); }  int fun(int foo, int bar){ return foo+bar; } 

will there (possibly) confusion (by either human or complier!) regarding conflicting names foo , bar, 1 in function definition, 1 in main program body? far find result correct, better know best practice. indeed run out of imagination, , name 2 occurrences (as in example) same.

(i have wondered years; basic sure must have asked before in long history of so, cannot find now. if duplicated, means let me know.)

to compiler, each variable associated scope defined language standard there must not exist cases of mis-understanding. if in case @ compilers cannot decide scope of variable ambiguous compiling error raised.

example: same name, different scope.

int x = 10;  void func() {   // definition of local x below hides global x in current scope   int x = x; // local x assigned value of global x   x = 5; // local x assigned 5, not affect global x   printf("local x = %d\n", x); }  int main() {   func();    printf("global x = %d\n", x);    return 0; } 

to see how compilers can treat variables same name different scope, read more @ how c compiler differentiate local , global variable of same name?

to human, it's called naming convention. free use valid names name variables recommended follow consistent rules - make code lot more readable , maintainable.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -