0% found this document useful (0 votes)
36 views

Variable Scope in C++, Reference Variable in C++

This document discusses variable scope and reference variables in C++. It defines three scopes for variables: local, global, and formal parameters. Local variables are declared inside functions and blocks while global variables are declared outside functions. Reference variables provide an alternative name for an existing variable and must be initialized when declared. The document provides examples demonstrating local and global variables, passing variables by reference in functions, and the differences between reference variables and pointer variables in C++.

Uploaded by

Anubhav Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Variable Scope in C++, Reference Variable in C++

This document discusses variable scope and reference variables in C++. It defines three scopes for variables: local, global, and formal parameters. Local variables are declared inside functions and blocks while global variables are declared outside functions. Reference variables provide an alternative name for an existing variable and must be initialized when declared. The document provides examples demonstrating local and global variables, passing variables by reference in functions, and the differences between reference variables and pointer variables in C++.

Uploaded by

Anubhav Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Variable Scope in C++, Reference Variable in

C++
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY

School Of Computer
Engineering

Mr. Abhaya Kumar Sahoo


Assistant Professor [II]
School of Computer Engineering,
Kalinga Institute of Industrial Technology (KIIT),
Deemed to be University,Odisha

3 Credit Lecture Note 05


Chapter Contents
2

 Variable Scope in C++


 Reference Variable in C++
Scope of Variable
3
A scope is a region of the program and broadly speaking there are three places, where
variables can be declared −
 Inside a function or a block which is called local variables,
 In the definition of function parameters which is called formal parameters.
 Outside of all functions which is called global variables.

Local Variables
 Variables that are declared inside a function or block are local variables.
 They can be used only by statements that are inside that function or block of code.
 Local variables are not known to functions outside their own. Following is the example
using local variables −
local Variables
4
 The C++ switch statement executes one statement from multiple conditions. It is like if-
else-if ladder statement in C++.
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a, b;
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
O/P: 30
Global Variables
5
Global Variables
 Global variables are defined outside of all the functions, usually on top of the program.
 The global variables will hold their value throughout the life-time of your program.
 A global variable can be accessed by any function.
 That is, a global variable is available for use throughout your entire program after its
declaration. Following is the example using global and local variables −
Example
6

#include <iostream>
using namespace std;
// Global variable declaration:
int g;
int main () {
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;

cout << g;

return 0;
}
local and global variables
7

#include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int main () {
// Local variable declaration:
int g = 10;
cout << g;

return 0;
}
References in C++
8

 When a variable is declared as reference, it becomes an alternative name for an existing


variable.
 A variable can be declared as reference by putting ‘&’ in the declaration.
 References are like constant pointers that are automatically dereferenced.
 It is a new name given to an existing storage. So when you are accessing the reference,
you are actually accessing that storage.
 A reference variable must be initialized at the time of declaration. This establishes the
correspondence between the reference and the data object which it names.

int main()
{
int y=10;
int &r = y; // r is a reference to int y
cout << r;
}
O/P: 10
here is no need to use the * to dereference a reference variable.
References in C++
9

#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x; O/P: x=20
// Value of x is now changed to 20 ref= 30
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}
Example (pass by reference)
10

#include <iostream>
using namespace std;
void swap(int& x, int& y) O/P:
{
Before Swap
a = 45 b = 35
int z = x;
After Swap with pass
x = y;
by reference
y = z;
a = 35 b = 45
}
int main()
{
int a = 45, b = 35;
cout << "Before Swap\n";
cout << "a = " << a << " b = " << b << "\n";
swap(a, b);
cout << "After Swap with pass by reference\n";
cout << "a = " << a << " b = " << b << "\n";
}
Reference variable Vs pointer variable
11

 References are generally implemented using pointers.


 A reference is same object, just with a different name and reference must refer to an
object. Since references can’t be NULL, they are safer to use.
1. A pointer can be re-assigned while reference cannot, and must be assigned at
initialization only.
2. Pointer can be assigned NULL directly, whereas reference cannot.
3. Pointers can iterate over an array, we can use ++ to go to the next item that a pointer is
pointing to.
4. A pointer is a variable that holds a memory address. A reference has the same memory
address as the item it references.
5. A pointer to a class/struct uses ‘->'(arrow operator) to access it’s members whereas a
reference uses a ‘.'(dot operator)
6. A pointer needs to be dereferenced with * to access the memory location it points to,
whereas a reference can be used directly.
12

You might also like