0% found this document useful (0 votes)
37 views

Computer Science ? HW

Variable is a name assigned to a memory location that can be manipulated by a program. Variables have a type that determines their size, value range, and operations. A variable's scope refers to the region of code where it is accessible. Local variables are declared within functions and only exist within that scope, while global variables are declared outside functions and exist throughout the program. Local variables do not affect other functions, while global variables can impact the entire program.

Uploaded by

Suraj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Computer Science ? HW

Variable is a name assigned to a memory location that can be manipulated by a program. Variables have a type that determines their size, value range, and operations. A variable's scope refers to the region of code where it is accessible. Local variables are declared within functions and only exist within that scope, while global variables are declared outside functions and exist throughout the program. Local variables do not affect other functions, while global variables can impact the entire program.

Uploaded by

Suraj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COMPUTER SCIENCE 🖥️🌐

HOLIDAY HOMEWORK
WHAT IS A VARIABLE?

• 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 which need to be stored inside that memory and nature
of operations that can be applied to that variable
SCOPE OF VARIABLES

• The scope of the variable is simply lifetime of a variable. It is block of code under which a
variable is applicable or alive. For example:
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.
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 block 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

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


GLOBAL VARIABLE

• A 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

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


LOCAL VARIABLE VS GLOBAL VARIABLES
• Global Variable Local Variable

Local Variables are declared


Global variables are
within a function block
declared outside all the
function blocks
The scope is limited and remains within
The scope remains the function only in which they are
throughout the program. declared.

Any change in global variable Any change in the local


affects the whole program, variable does not affect other
wherever it is being used. functions of the program

A global variable exists in A local variable is created when


the program for the entire the function is executed, and
time the program is once the execution is finished,
executed. the variable is destroyed.

You might also like