Home | | Computer Science 11th std | C++: Passing structures to functions

Example Program - C++: Passing structures to functions | 11th Computer Science : Chapter 12 : Arrays and Structures

Chapter: 11th Computer Science : Chapter 12 : Arrays and Structures

C++: Passing structures to functions

A structure variable can be passed to a function in a similar way of passing any argument that is of built-in data type.

Passing structures to functions

 

A structure variable can be passed to a function in a similar way of passing any argument that is of built-in data type.

If the structure itself is an argument, then it is called “call by value”. If the reference of the structure is passed as an argument then it is called, “call by reference”.

 

1. Call by value.

 

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.

 

Consider this example:

#include <iostream>

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 printData(). The argument for the above function is the structure Employee. The input can be received through a function named readData().

#include <iostream>

using namespace std;

struct Employee {

char name[50];

int age;

float salary;

};

Employee readData(Employee);

void printData(Employee);

int main()

{

      Employee p;

      p = readData(p);

      printData(p);

return 0;

}

      Employee readData(Employee p) {

      cout<< "Enter Full name: ";

      cin.get(p.name, 50);

      cout<< "Enter age: ";

      cin>>p.age;

      cout<< "Enter salary: ";

      cin>>p.salary;

return p;

}

void printData(Employee p)

{

      cout<< "\nDisplaying Information." <<endl;

      cout<< "Name: " << p.name <<endl;

      cout<<"Age: " <<p.age<<endl;

      cout<< "Salary: " <<p.salary;

}

The output of the program is the same as that of the previous programme.

 

2. Call by reference

 

In this method of passing the structures to functions ,the address of a structure variable /object is passed to the function using address of(&) operator. So any change made to the contents of structure variable inside the function are reflected back to the calling function.

#include <iostream>

using namespace std;    

struct Employee {

char name[50];

int age;

float salary;

};

void readData(Employee &);

void printData(Employee);

int main() {

      Employee p;

       readData(p);

      printData(p);

return 0;

}

void readData(Employee &p) {

      cout<< "Enter Full name: ";

      cin.get(p.name, 50);

      cout<< "Enter age: ";

      cin>>p.age;

      cout<< "Enter salary: ";

      cin>>p.salary;

}

void printData(Employee p)

{

      cout<< "\nDisplaying Information." <<endl;

      cout<< "Name: " << p.name <<endl;

      cout<<"Age: " <<p.age<<endl;

      cout<< "Salary: " <<p.salary;

}

Output:

Enter Full name: Kumar

Enter age: 55

Enter salary: 34233.4

Displaying Information.

Name: Kumar

Age: 55

Salary: 34233.4

Structures  are  usually  passed  by reference method because it saves the memory space and executes faster.

 

3. Returning Structures from Functions

 

Astructure can be passed to a function through its object. Therefore, passing a structure to a function or passing a structure object to a function is the same because structure object represents the structure. Like a normal variable, structure variable(structure object) can be passed by value or by references / addresses.

Similar to built-in data types, structures also can be returned from a function.

#include<iostream.h>

using namespace std;

struct Employee

{

int Id;

char Name[25];

int Age;

long Salary;

};

      Employee Input();

void main()

{

      Employee e;

      Emp = Input();

      cout<< “The values Entered are”<<endl:

      cout<< "\n\nEmployee Id : " <<e.Id;

      cout<< "\nEmployee Name : " <<e.Name;

      cout<< "\nEmployee Age : " <<e.Age;

      cout<< "\nEmployee Salary : " <<e.Salary;

}

      Employee Input()

{

      Employee e;

      cout<< "\nEnter Employee Id : ";

      cin>>e.Id;

      cout<< "\nEnter Employee Name : ";

      cin>>e.Name;

      cout<< "\nEnter Employee Age : ";

      cin>>e.Age;

      cout<< "\nEnter Employee Salary : ";

      cin>>e.Salary;

return;

}

Output :

Enter Employee Id : 10

Enter Employee Name : Ajay

Enter Employee Age : 25

Enter Employee Salary : 15000

Employee Id : 10

Employee Name : Ajay

Employee Age : 25

Employee Salary : 15000

 

Tags : Example Program , 11th Computer Science : Chapter 12 : Arrays and Structures
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 12 : Arrays and Structures : C++: Passing structures to functions | Example Program


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.