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

Global Variables

Global variables are defined outside functions and can be accessed throughout a program, offering convenience and efficiency but also introducing issues like namespace pollution and code complexity. Best practices for using global variables include using them sparingly, employing meaningful names, and encapsulating them to avoid conflicts. Alternatives to global variables include using function arguments, return values, local variables, and object-oriented programming techniques.

Uploaded by

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

Global Variables

Global variables are defined outside functions and can be accessed throughout a program, offering convenience and efficiency but also introducing issues like namespace pollution and code complexity. Best practices for using global variables include using them sparingly, employing meaningful names, and encapsulating them to avoid conflicts. Alternatives to global variables include using function arguments, return values, local variables, and object-oriented programming techniques.

Uploaded by

ollahmagara
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Global Variables

These are variables that are created outside of a function are known as global variables and have a
global scope.

Characteristics of global variables

Accessibility: Global variables are accessible throughout the program and inside every function.

Scope: Global variables have a global scope, meaning they can be accessed from any part of the
program.

Global variables can be used both inside of functions and outside.

Lifetime: Global variables have a lifetime which exists for the duration of the program’s execution.

Advantages of global variables

1. Convenience : Global variables can be accessed from anywhere in the program, making it easier to
share data between functions.

2. Efficiency: global variables can reduce the need for function arguments and return values, making the
code more efficient.

Disadvantages of global variables

1. Namespace pollution: global variables can clutter the global namespace , making it harder to
find and use variables.
2. Code complexity: global variables can make the code harder to understand and maintain, as
their values can be changed from anywhere in the program.

Best practices for using global variables

1. Use sparingly: Use global variables only where , and prefer loval variables or function instead
arguments instead.
2. Use meaningful names : Use descriptive and meaningful names for global variables to avoid
confusion.
3. Avoid mutable global variables as they can lead to thread safety issues and code complexity.
4. Use encapsulation: Encapsulate global variables within a namespace or a class to avoid
namespace pollution.

Alternatives to global variables


1. Function arguments: pass variables as function arguments instead of using global variables.
2. Return values :Use return values instead of modifying global variables .
3. Local variables: Use local variables instead of global variables whenever possible.
4. Object oriented programming: Use object oriented programming techniques like inheritance to
avoid global variables.

Example

Creating a global variable


def f():

print(“inside function”, s )

s= “I love you”

f()

print(“outside function”, s )

Variable s is used both inside and outside as well

If you create a variable with the same name inside a function, this variable will be local, and can only be
used inside the function. The global variable with the same name will remain as it was, global and with
the original value.

You might also like