answersLogoWhite

0

What variable scope is preferable?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

Shorter lifespan, the better (preferable): local.

In some languages, local means within a {-} or begin-end pair, not necessarily a method/function. For example:

void MyDummyOperation() {

int i;

// 10+ lines of codes before the first time of using i

}

void LocalDemo() {

if (1==2) { int i = 0; ....}

}

Notice the local variable int i is declared at the true-branch of the if-statement (although the condition will not be evaluated to true) in LocalDemo() method. That variable cannot be seen outside of that scope, while the one in MyDummyOperation() has a life span of the entire function.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What variable scope is preferable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What variable scope is preferable in modules to avoid overwriting existing data?

C: static other: (default)


What is the scope of any variable?

The Scope of a variable defines the areas of a program where this variable would be visible and can be used. For ex: a. Method variables - are visible only inside the method where they are declared and hence their scope is only the method b. Class variables - are visible inside the class and can be used by any method inside the class and hence their scope is the whole class.


What is out of scope in returning objects in java?

if u declare variable in method & tray to use this variable outside the method then it is out of scope


In most languages where does a local variables scope begin and end?

A local variable only exists within the scope in which it is declared. As soon as the scope ends, the variable ceases to exist. { // beginning of a scope, i does not yet exist int i = 42; // local variable declared, i now exists } // end of scope, i no longer exists


Differences between scope and visibility of a variable?

Scope of a variable is the lines of code from which it can be seen and/or manipulated. Scope can be thought of as visibility, because a variable's scope is where it is visible from. A variable declared within a function (or any block, for that matter) has scope only within that block. It is visible within that block, and from within any contained block, but not from within any containing (outer) blocks. However, it should be noted that if a variable's name is reused in a nested declaration, then the outer variable loses scope within that block. As a result, you can reuse, for instance, the variable "i" within a new block without compromising any use outside that block.

Related questions

What variable scope is preferable in modules to avoid overwriting existing data?

C: static other: (default)


Scope of static variables?

Scope of static variable is with in the file if it is static global. Scope of static variable is with in the function if variable is declared local to a function. But the life time is throughout the program


What is the scope of any variable?

The Scope of a variable defines the areas of a program where this variable would be visible and can be used. For ex: a. Method variables - are visible only inside the method where they are declared and hence their scope is only the method b. Class variables - are visible inside the class and can be used by any method inside the class and hence their scope is the whole class.


What is the scope of a data statement?

The scope of a data statement refers to where in the program the data is accessible. It defines the visibility and accessibility of the data within a program, determining which parts of the program can access and manipulate the data. Properly scoping data helps in managing data effectively and preventing unintended side effects.


Which specifier indicates that a variable's reserved space in the memory can be erased or relocated when the variable is out of its scope?

There is no such thing. When the program leaves the scope of the variable, it will be release the memory of the variable automatically and unconditonally.


What is out of scope in returning objects in java?

if u declare variable in method & tray to use this variable outside the method then it is out of scope


Is possible to declare a variable in a method and declare it a main?

The same identifier (variable name) may be used for at most one variable in each scope. Each method has its own scope, in addition to the global scope which is accessible from all others. However, each scope would have a different variable than every other scope despite using the same name for it.


In most languages where does a local variables scope begin and end?

A local variable only exists within the scope in which it is declared. As soon as the scope ends, the variable ceases to exist. { // beginning of a scope, i does not yet exist int i = 42; // local variable declared, i now exists } // end of scope, i no longer exists


What brand of scope is on the Remington 770?

A Bushnell 3-9x40 variable scope


Explain different types of coding style in software engineering?

Meaningful variable names. Always use descriptive and meaningful variable names. Don't worry about the length. Meaningful variable names help other people reading your code understand what is going on. Example: if (a <= = 24) What is a? This is confusing. if (vacationHours <= = 24) See, better. <li>Variable scope indicated by name. Code is easier to read and understand if each variable indicates it's scope. One way to do this is to add prefixes to variable names, for example: Scope = Class member or instance variable: m_hours Scope = Class static variable: s_hours Scope = Parameter passed in a method call: p_hours Scope = local variable to a method: l_hours Regards ~Anoop


What does an independent variable depend on in math?

It can depend on nothing or on an variable which is outside of the scope of the study.


What is the best variable rifle scope you can get for 250 or less?

Nikon Prostaff is an excellent variable scope for under $250. Excellent optics, can't beat it for the price.