What happens when I name a local variable the same thing as an existing global variable?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What happens when I name a local variable the same thing as an existing global variable??

If I globally declare a variable and then use same name of the variable in a function which could be local variable. Then want will happen??

10th May 2019, 6:53 PM
Sumit Sinha
Sumit Sinha - avatar
4 Answers
+ 1
You'll hide the global variable due local scope is primary.
10th May 2019, 7:01 PM
Daniel Adam
Daniel Adam - avatar
+ 1
I already gave you the answer.
11th May 2019, 6:19 AM
Daniel Adam
Daniel Adam - avatar
0
Within a class you can specify the scope with this.* , or base.* (C#).
10th May 2019, 7:02 PM
Daniel Adam
Daniel Adam - avatar
0
Ok, but what happen when you declare the variable globally and you the name of variable as a parameter in a function. Suppose var balance = 10500; and function steal(balance, amount) { if(amount < balance) { balance = balance - amount; } return amount; } var amount = steal(balance, 1250);
10th May 2019, 7:52 PM
Sumit Sinha
Sumit Sinha - avatar