Professional Documents
Culture Documents
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 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.
#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
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.
struct tag
{
data_type1 member1;
data_type2 member2;
data_type3 member3;
};
struct <structure_name>
{
data_type1 member1;
data_type2 member2;
data_type3 member3;
...
...
};
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.
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)
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.
struct student
{
int roll_no;
char name[20];
}stu;
struct student
{
int roll_no;
char name[20];
};
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
. struct to member
--> pointer to member
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.
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.
struct student
{
char name[20];
int roll;
};
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