Scope

Scope in programming is the space in code where a variable is both defined and visible. Scope differentiates regions of a program and the way the program defines and uses variables. If a variable has one specific definition throughout the entire program, regardless of the function in which it appears, its scope is global. Globally-scoped variables are the same in all of the source code. In contrast, local scope defines a variable for one particular function; it only exists within that function.

The definition of scope is a bit misleading; it also refers to how a variable functions within a program, not just the area in which the variable is definable and visible. The term scope indicates just the area covered, not the action performed; researchers and developers understand the limitations of the term and understand that scope means both the definition and behavior of a variable.

Static vs. dynamic scope

Static (lexical) and dynamic scope are two kinds of scope that occur in programming; static is by far the more common. If a program scopes statically, or lexically, a variable within a function will return its assigned value for that scope whenever that function runs. For example, if a variable x = 5 in a given function, and a subsequent conditional definition for x is also provided, x = 5 wherever the program’s scope defines it as such.

Dynamic scope, however, calls the most recent definition of that variable, regardless of the program’s structure. In the above example, if x = 5 but then x was later defined with a value of 10, a program with a dynamic scope would call x = 10 if that value had been most recently used. Dynamic scope is less structured than static scope; it pulls variable definitions from the call stack instead of the area in which it would be logically defined.






Jenna Phipps
Jenna Phipps
Jenna Phipps is a writer for Webopedia.com, Enterprise Storage Forum, and CIO Insight. She covers data storage systems and data management, information technology security, and enterprise software solutions.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.

Related Articles

Embedded Analytics

Embedded analytics brings self-service business intelligence to everyday application users.

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their workforce operations. They provide a central platform for human resources professionals...

Complete List of Cybersecurity Acronyms

Cybersecurity news and best practices are full of acronyms and abbreviations. Without understanding what each one means, it's difficult to comprehend the significance of...

Human Resources Management System

A Human Resources Management System (HRMS) is a software application that supports many functions of a company's Human Resources department, including benefits administration, payroll,...

ScalaHosting

ScalaHosting is a leading managed hosting provider that offers secure, scalable, and affordable...

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their...

Best Managed Service Providers...

In today's business world, managed services are more critical than ever. They can...