You are on page 1of 8

Call by Value and Call by Reference in C++

On the basis of arguments there are two types of function are available in C++ language, they
are;

 With argument
 Without argument

If a function take any arguments, it must declare variables that accept the values as a arguments.
These variables are called the formal parameters of the function. There are two ways to pass
value or data to function in C++ language which is given below;

 call by value
 call by reference
Call by value
In call by value, original value can not be changed or modified. In call by value, when you
passed value to the function it is locally stored by the function parameter in stack memory
location. If you change the value of function parameter, it is changed for the current function
only but it not change the value of variable inside the caller function such as main().

Call by value

#include<iostream.h>
#include<conio.h>

void swap(int a, int b)


{
int temp;
temp=a;
a=b;
b=temp;
}

void main()
{
int a=100, b=200;
clrscr();
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

Output

Value of a: 200
Value of b: 100

Call by reference
In call by reference, original value is changed or modified because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments shares the
same address space. Hence, any value changed inside the function, is reflected inside as well as
outside the function.

Example Call by reference

#include<iostream.h>
#include<conio.h>
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

void main()
{
int a=100, b=200;
clrscr();
swap(&a, &b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

Output

Value of a: 200
Value of b: 100

Difference between call by value and call by reference.

call by value call by reference


This method copy original value into This method copy address of arguments into
1
function as a arguments. function as a arguments.
Changes made to the parameter affect the argument.
Changes made to the parameter inside the
2 Because address is used to access the actual
function have no effect on the argument.
argument.
Actual and formal arguments will be Actual and formal arguments will be created in same
3
created in different memory location memory location

Note: By default, C++ uses call by value to pass arguments.

Structure in C++
Structure is a user defined data type which hold/store different-different type of data
Item/Element in a singe variable. In case of array we store only similar data type.

Declare Structure in C++

struct tag
{
data_type1 member1;
data_type2 member2;
data_type3 member3;
};

Another syntax to Declare Structure.

Declare Structure in C++

struct <structure_name>
{
data_type1 member1;
data_type2 member2;
data_type3 member3;
...
...
};

Why Use Structure in C++


In C++ language Array is also a user defined data type but Array hold or store only similar type
of data, If we want to store different-different type of data. In this case we need to defined
separate variable for each type of data. To resolve this problem we use Structure in C++
Programming, It can store different-different data type in single variable.

Example: Suppose we want to store Student record, then we need to store....

 Student Name
 Roll number
 Class
 Address

For store Student name and Address we need character data type, for store Roll number and class
we need integer data type.

If you use Array, in this case you need to defined separate variable like below example.

Example
char student_name[10], address[20];
int roll_no[5], class[5];

If we use Structure then we use single variable for all data like belowe example.

Example
struct stu
{
char student_name[10];
char address[20];
int roll_no[5];
int class[5];
};

Note: Minimum size of Structure is one byte and Maximum size of Structure is sum of all
members variable size.

Note: Empty Structure is not possible in C++ Language.

Defining a Structure
Syntax
struct tagname
{
Datatype1 member1;
Datatype2 member2;
Datatype3 member3;
.........
};

At end of the structure creation (;) must be required because it indicates that an entity is
constructed.

Example
struct emp
{
int id;
char name[36];
int sal;
};
sizeof(struct emp) // --> 40 byte (2byte+36byte+2byte)

Syntax to create structure variable


struct tagname variable;

Difference Between Array and Structure

Array Structure
Array is collection of homogeneous data. Structure is the collection of heterogeneous data.
Structure elements are access using . (dot)
Array data are access using index.
operator.
Array allocates static memory. Structures allocate dynamic memory.
To access Array element need less time To access Structure elements takes more time
compare to structures. compare to Array.

Some Important Points Regarding Structure in C++

 Struct keyword is used to declare structure.


 Members of structure are enclosed within opening and closing braces { }.
 Declaration of Structure reserves no space.
 It is nothing but the "Template / Map / Shape" of the structure .
 Memory is created, very first time when the variable is created or Instance is created.

Different Ways of Declaring Structure Variable

Declare Structure Variable Immediately after Structure


Template in C++

struct student
{
int roll_no;
char name[20];
}stu;

// 'stu' is name of Structure variable

Declare Structure Variables using struct Keyword in C++

struct student
{
int roll_no;
char name[20];
};

struct student stu; // here stu is name of variable

Note: Where "student" is name of structure and stu is name of variable.

Declaring Multiple Structure Variables in C++

struct Books
{
int pages;
char name[20];
char author[50];
char subject[100];

}book1,book2,book3;

Note: We can declare multiple variables separated by comma directly after closing curly }.

Syntax to access structure members

By using following operators we can access structure members.

Syntax
. struct to member
--> pointer to member

Accessing Structure Members

To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access.

Example of Structure in C++


#include<iostream.h>
#include<conio.h>

struct emp
{
int id;
char name[36];
float sal;
};
void main()
{
struct emp e; // create structure variable, Here e is variable
clrscr();
cout<<"Enter employee Id, Name, Salary: ";
cin>>e.id;
cin>>e.name;
cin>>e.sal;

cout<<"Id: "<<e.id<<endl;
cout<<"Name: "<<e.name<<endl;
cout<<"Salary: "<<e.sal;
getch();
}

Output
Output:
Enter employee Id, Name, Salary:
5
Gaurav
42600
Id : 05
Name: Gaurav
Salary: 42600.00

When the variable is normal type then go for struct to member operator.

When the variable is pointer type then go for pointer to member operator.

Pass Structure to a Function in C++

A structure variable can be passed to the function as an argument as a normal variable. If


structure is passed by value, changes made to the structure variable inside the function definition
does not reflect in the originally passed structure variable.

Pass Structure in Function in C++


#include<iostream.h>
#include<conio.h>

struct student
{
char name[20];
int roll;
};

void display(struct student stu);

void main()
{
struct student stud;
cout<<"Enter student's name: ";
cin>>stud.name;
cout<<"Enter roll number:";
cin>>stud.roll;
display(stud); // passing structure variable stud as argument
getch();
}
void display(struct student stu)
{
cout<<"Name: "<<stu.name<<endl;
cout<<"Roll: "<<stu.roll;
}
Output
Enter student's name: Hitesh Porter
Enter roll number: 18
Name: Hitesh Porter

You might also like