0% found this document useful (0 votes)
50 views10 pages

Bca Notes CPP

The document discusses the scope of variables in C++, explaining that it defines the region of the program where a variable can be accessed. It also contrasts 'Call by Value' and 'Call by Reference', detailing how they differ in terms of memory usage, performance, and application. Additionally, it introduces C++ structures, which allow for the creation of user-defined data types to group different data types together.

Uploaded by

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

Bca Notes CPP

The document discusses the scope of variables in C++, explaining that it defines the region of the program where a variable can be accessed. It also contrasts 'Call by Value' and 'Call by Reference', detailing how they differ in terms of memory usage, performance, and application. Additionally, it introduces C++ structures, which allow for the creation of user-defined data types to group different data types together.

Uploaded by

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

Scope of Variables

in C++
Last Updated : 23 Jul, 2025

In C++, the scope of a variable is the


extent in the code upto which the variable
can be accessed or worked with. It is the
region of the program where the variable is
accessible using the name it was declared
with.
Let's take a look at an example:

#include <iostream>
using namespace std;

// Declaring first variable


int a = 10;

int main() {
// Declaring second variable
int b = 9;

// Accessing a and b variable in their scope


cout << a << " " << b;

return 0;
}

Output
10 9
Explanation: The variables a and b are
declared in some part of the program and
accessed in other part. It is possible
because the accessing is done in the scope
where the variables are valid.

Difference Between
Call by Value and
Call by Reference in
C++
Last Updated : 23 Jul, 2025

Call by Call by
Feature Value Reference
In this
In call by
method, the
reference,
Value value of the
reference to
Passed variable is
the variable
passed to
is passed.
the function.

Scope of The original The


Changes value changes
remains made are
unchanged reflected in
even when the original
we make variable.
Call by Call by
Feature Value Reference
changes in
the function.

It requires It is more
extra memory and
memory and time efficient
Performance
time to copy as it does
so less not create a
efficient. copy.

The actual
The memory and the
addresses formal
Memory of the actual parameters
Location and formal point at the
parameters same
are different. memory
address.
Call by Call by
Feature Value Reference
Mainly used
It is used
to pass
when we
values for
want to
small data
modify the
Applications or when we
original
do not want
value or
to change
save
original
resources.
values.
Call by Value in C++
In Call by Value, the actual value of the
argument is passed to the function. A copy
of the argument is made, and the function
works on this copy. The original variable in
the calling function remains unaffected by
any changes made to the parameter inside
the function.
Example:
#include <iostream>
using namespace std;

// Function to update the original value


void increment(int n){
n++;
cout << n << endl;
}

int main()
{
int num = 5;
// Passing 'number' by value
increment(num);
cout << num << endl;
return 0;
}

Output
6
5
Explanation: In the above program the
"number" is passed as an actual parameter
to the function increment and in the
function as a formal parameter "num" that
stores the value of "number". In the
function, the value of "num" is incremented
but there is no change in the value of
"number". This is what we say "call by
value".
Call by Reference in C++
In Call by Reference, instead of passing a
copy of the argument, the memory address
(reference) of the variable is passed to the
function. This allows the function to directly
modify the original argument, as both the
formal and actual parameters refers to the
same variable.
Example:

#include <iostream>
using namespace std;

// function to update the original value


void increment(int& num)
{
num++;
cout << num << endl;
}

int main()
{
int number = 5;
// Passing 'number' by reference
increment(number);
cout << number << endl;
return 0;
}

Output
6
6
Explanation: In the above code the
variable "number" is passed to function
increment. A formal parameter "num"
points to the same address as "number".
When the function increments the value of
the parameter, the changes made in "num"
is reflected in "number" as we can see from
the output. This is what we say "call by
reference".
Structures in C++
Last Updated : 28 Jul, 2025

C++ Structures are used to create user


defined data types which are used to store
group of items of different data types.
Syntax
Before using structure, we have to first
define the structure using
the struct keyword as shown:
struct name{
type1 mem1;
type2 mem2;
...
};
where structure name
is name and mem1, mem2 and mem3 are
the items it groups. They are also called
its members or fields.
Example:
Struct
ure
The above is also called Structure
Definition. It is not allocated any memory
and cannot be used in the program directly.
We have to create its variables to use it.

You might also like