> For the complete documentation index, see [llms.txt](https://university-west.gitbook.io/programming-paradigms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/scope.md).

# 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

```python
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

```csharp
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/scope.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
