General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals

Scope

In computer programming, scope refers to the region of the code where a particular variable or function is accessible. Scope management is crucial for avoiding naming conflicts and ensuring that the program behaves as expected. There are generally two main types of scope: global and local.

  • Global Scope: A variable defined in the global scope is accessible from any other part of the code.

  • Local Scope: A variable defined in a local scope, such as within a function, is only accessible within that function.

Python Example

x = "global"

def function():
    x = "local"
    print(x)  # prints "local"

function()
print(x)  # prints "global"

In this Python example, there are two x variables: one in the global scope and one in the local scope of function(). When print(x) is called inside function(), it prints local because the function scope's x shadows the global x. Outside of function(), the global x is printed.

C# Example

using System;

class Program
{
    static string x = "global";  // Global scope variable
    
    static void Main(string[] args)
    {
        string x = "local";  // Local scope variable
        
        Console.WriteLine(x);  // prints "local"
    }
    
    static void PrintGlobal()
    {
        Console.WriteLine(x);  // prints "global"
    }
}

In the C# example, similar to the Python example, there's a global x variable and a local x variable within the Main method. The Console.WriteLine(x); in Main prints local because the local x shadows the global x. However, in the PrintGlobal method, since there's no locally scoped x, the global x is printed when the method is called.

Understanding scope is essential for writing effective and error-free code, as it helps to manage where and how variables and functions can be accessed and modified.

PreviousSpace and TimeNextStandard libraries

Last updated 1 year ago

Was this helpful?