An advantage of using local variables is that _____

they are known to all functions in the file
names used in one function do not affect data stored in variables with the same names in other functions
values given to local variables are retained when those parameters go out of scope

The correct answer is: B. names used in one function do not affect data stored in variables with the same names in other functions.

A local variable is a variable that is only accessible within the function in which it is declared. This means that if a function has a local variable called x, then another function in the same file cannot access that variable. This is useful because it prevents one function from accidentally changing the value of a variable that is being used by another function.

Option A is incorrect because local variables are not known to all functions in the file. Only the function in which they are declared can access them.

Option C is incorrect because values given to local variables are not retained when those parameters go out of scope. The scope of a variable is the part of the program in which it can be accessed. When a function ends, all of its local variables go out of scope and their values are lost.

Option D is incorrect because using local variables does not prevent the program from becoming “crowded” with too many variable names. In fact, using local variables can help to make the program more readable and maintainable by reducing the number of global variables that need to be tracked.

Exit mobile version