Learning C++: Variable Scope

Learning C++: Variable Scope

Variable scope refers to the ability of a variable to be “seen” in a program. A variable is seen if, in a specific place in a program, the programmer has access to that variable’s value.

Variable scope refers to the ability of a variable to be “seen” in a program. A variable is seen if, in a specific place in a program, the programmer has access to that variable’s value. There are levels of scope in a C++ program, with the level of scope determining how widely a variable can be seen in a program. In this article I’m going to discuss the different levels of variable scope and why they are important.

The Scope Levels

Here are the three levels of scope in a C++ program: global, local, and block. Global is the most expansive scope and means that any variable declared with global scope can be seen in any place in a program. Local scope usually refers to a variable defined in a function and can be seen throughout that function, but not in other functions. Block scope means a variable was defined within a block of code, such as a for loop or an if statement.

With these definitions out of the way, let’s look at how these different scope levels affect a C++ program.

Global Scope

A variable that has global scope can be seen in any other part of a C++ program, from a function definition to a block of code. A variable that is defined outside of the main function is a global variable.

Here is an example that demonstrates how global scope works:

#include <iostream>
using namespace std;

// global space
int number = 1;
void showValue() {
  cout << "Number accessed from a function: " << number << endl;
}
int main ()
{
  showValue();
  cout << "Number accessed from the main function: "
       << number << endl;
  for (int i = 1; i <= 1; i++) {
    cout << "Number accessed from a block: " << number << endl;
  }
  return 0;
}

programming-education learn-to-program cpp learn-to-code programming deep learning

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Deep Learning Development Services | Hire Deep Learning Developer

Inexture's Deep learning Development Services helps companies to develop Data driven products and solutions. Hire our deep learning developers today to build application that learn and adapt with time.

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

Learning to Code: How to Boost Up the Process?

Learning to Code: How to Boost Up the Process? I also often recommend different online and offline resources to my students to make their learning process easier, more effective, and faster. And in this post, I will share a few tips with you.

The Most Important Programming Lesson I Ever Learned

The most important lesson in programming is learning to debug code. Here is a list of my favorite debugging tips and tricks in JavaScript, Python, and more.

Learning C++: Table-Driven Selection

The typical way to perform selection (or branching) in C++ is with the if statement. In this article I’m going to demonstrate several ways to replace complex if statements with tables.