Can we declare a variable in both the inner and the outer block of my program?Why it is called a bad practice if programatically it doesn’t create any problem..
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can declare variables either in functions (local variables), or outside functions (global variables). You should generally minimize the scope of a variable, as this makes the code easier to understand.
For example, if you’re modifying the value of a global variable in one function, and call another function which, for unrelated reasons, also happens to modify that variable, the resulting code might do things that you’re not expecting and which someone reading your code wouldn’t expect (let alone if you were to then try parallelising that code…)