When a structure is passed as argument to a function using call by value method, any change made to the contents of the structure variable inside the function to which it is passed do not affect the structure variable used as an argument.
#include
using namespace std;
struct Employee
{
char name[50];
int age;
float salary;
};
void printData(Employee); // Function declaration
int main()
{
Employee p;
cout << "Enter Full name:";cin>>p.name;
cout << "Enter age :";cin>>p.age;
cout << "Enter salary:";cin>>p.salary;
// Function call with structure variable as an argument
printData(p);
return 0;
}
void printData(Employee q)
{
cout << “\nDisplaying Information.” << endl;
cout << “Name:” << q.name << endl;
cout <<”Age:” << q.age << endl;
cout << “Salary:” << q.salary;
}
Output:
Enter Full name: Kumar
Enter age : 55
Enter salary : 34233.4
Displaying Information.
Name : Kumar
Age : 55
Salary : 34233.4
In the above example, a structure named Employee is declared and used. The values that are entered into the structure are name, age and salary of a Employee are displayed using a function named print Data(). The argument for the above function is the structure Employee. The input can be received through a function named readData().