Computer Science
What is the difference between local and global variable ?
Python Functions
5 Likes
Answer
Local variable | Global variable |
---|---|
A local variable is a variable defined within a function. | A global variable is a variable defined in the 'main' program. |
They are only accessible within the block in which they are defined. | They are accessible from anywhere within the program, including inside functions. |
These variables have local scope. | These variables have global scope. |
The lifetime of a local variable is limited to the block of code in which it is defined. Once the execution exits that block, the local variable is destroyed, and its memory is released. | Global variables persist throughout the entire execution of the program. They are created when the program starts and are only destroyed when the program terminates. |
Answered By
1 Like
Related Questions
Can a function return multiple values ? How ?
What is scope ? What is the scope resolving rule of Python ?
When is global statement used ? Why is its use not recommended ?
Write the term suitable for following descriptions :
(a) A name inside the parentheses of a function header that can receive a value.
(b) An argument passed to a specific parameter using the parameter name.
(c) A value passed to a function parameter.
(d) A value assigned to a parameter name in the function header.
(e) A value assigned to a parameter name in the function call.
(f) A name defined outside all function definitions.
(g) A variable created inside a function body.