Detail the categories of variables by lifetimes including their respective advantages and disadvantages.

 We need to know about what is variable.?

A Variable is a name assign to a storage area that the program can manipulate. A variable type determines the size and layout of the variable’s memory. It also determines the range of values that need to be stored inside that memory and the nature of operations that can be applied to that variable.

Scope of Variables

The scope of the variable is simply the life of a variable. It is a block of code under which a variable is applicable or alive. For example:

function foo(){
var x;
}

You declare a variable “x” inside a function “foo.” The scope of that variable remains inside that function it can’t be used outside of that function.

Categories of variables by lifetimes

There are three places where variables you can declare variable programming language:

  • Inside a function or a block: Local variables
  • Outside of all functions: Global variables
  • In the definition of function parameters: Formal parameters

Local Variable:-

local Variable is defined as a type of variable declared within programming blocks or subroutines. It can only be used inside the subroutine or code block in which it is declared. The local variable exists until the block of the function is under execution. After that, it will be destroyed automatically.

Example of Local Variable :

public int add(){
int a =4;
int b=5;
return a+b;
}

Here, ‘a’ and ‘b’ are local variables

Global Variable:

Global Variable in the program is a variable defined outside the subroutine or function. It has a global scope means it holds its value throughout the lifetime of the program. Hence, it can be accessed throughout the program by any function defined within the program, unless it is shadowed.

Example:

int a =4;
int b=5;
public int add(){
return a+b;
}

Here, ‘a’ and ‘b’ are global variables.

Advantages of using Global variables

  • You can access the global variable from all the functions or modules in a program
  • You only require to declare global variables a single time outside the modules.
  • It is ideally used for storing “constants” as it helps you keep consistency.
  • A Global variable is useful when multiple functions are accessing the same data.

Advantages of using Local Variables

  • The use of local variables offer a guarantee that the values of variables will remain intact while the task is running
  • Local variables are deleted as soon as any function is over and release the memory space which it occupies.

Disadvantages of using Global Variables

  • Too many variables are declared as global, then they remain in the memory till program execution is completed. This can cause Out of Memory issues.
  • If global variables are discontinued due to code refactoring, you will need to change all the modules where they are called.

Disadvantages of using Local Variables

  • The debugging process of a local variable is quite tricky.
  • Common data is required to pass repeatedly as data sharing is not possible between modules.
  • They have a very limited scope.

Comments

Popular posts from this blog

A recursive function in C++ must always have a void return type.

What do “Feasibility” and “Desirability” mean in the context of software design. Specify one question for each that leads to a design type.The main goal of a software design thinking work process is to create a solution that is desirable, feasible, and viable. This means our solution should not only provides a solution but also needs to implement easily and have a proper commercial model. Desirability is at the forefront as it is mainly concerned with our ideas and validating things about taking users in mind and also to do testing again and again. A test for desirability focuses on whether your solution is a nice to have or a must-have for your customer. Ask yourself, what task am I helping my customer complete? What does successful completion of that task look like for them? Looking at what they are trying to do and why it is important to them puts yourself in their shoes, lets you look at your solution from their eyes. If you are solving the key pain points they encounter when trying to complete this task, your solution has met the test for desirability. If not, and there are other pain points that you haven’t addressed, then pivoting your solution might put you on a better path. Feasibility in software design is a validation process that looks at how design delivers to requirements, objectives, and goals. The common Software design feasibility is: Technical Financial Usability Risk Experience Quality Feasibility is a way to evaluate the desirability of a project or a design. Any company before any project invests their time and money in any project they want to know how successful the project will be before investing. As the company wants to know how much amount of research is needed to be done and also to know the market value of a project.

Task To Lead a Developing Team