Scope of Variables in C++2 min read

In any programming language, the scope is a region which is can be defined as the extent up to which something is valid. All the variables used in a program have their extent of use and if used out of that extent or boundary, they do not hold their values. Hence this boundary is referred to as the scope of a variable

These boundaries are referred to curly braces {} in a program. Based on their scope, there are two types of variables.

  • Local variables: inside a function or a block.
  • Global variables: outside of all functions.
  • Formal parameters: function parameters.
  • block: refers to anything between ‘{‘ and ‘}’.

1. Local variables:

Local variables are the variables that are declared within the body of the function or block. These types of variables can only be used within a block where it is created or declared, it has no existence outside the block.

Example: C++ example using local variables.

Output:

Let us see one more example where the variable with same name can be used in different function holding different values.

Output:


2. Global Variable

These variables are available to use throughout the program that is they have their scope throughout the program. They are defined outside the function (including main()), usually, you will see them at the top of any program.

After its declaration in a program, a global variable can be used inside any function present in a program.

Example: C++ example using global variables.

Output:

Let us see one more example where the local variable takes preference.

Output:

As you can see in the above program, g is declared global with a value of 20, but again it is initialized inside testFunction() with the value of 30. And present value(30) overrides the previous value (20) and prints 30.

In the main function again g is defined and initialized (making it local variable) with 50 that overrides the 20.


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …