Loading...
C++ Structure and Function

C++ Structure and Function

In this tutorial, you'll find relevant examples to pass structures as an argument to a function, and use them in your program with the help of examples.


Passing structure to function

  • A Structure variables can be passed to a function and returned in a similar way as normal arguments.
  • A structure can be passed to any function from main() function or from any sub function.
  • Structure definition will be available within the function only.
  • It won't be available to other functions unless it is passed to those functions by value or by address (reference).

Ways to pass structure

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
  2. 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:


Example 1: C++ Structure and Function

#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.get(p.name, 50);
    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 p){
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout <<"Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}

Output

Enter Full name: Micheal Clark
Enter age: 20
Enter salary: 20000
                                                                      
Displaying Information.
Name: Micheal Clark
Age: 20
Salary: 20000

In this program, user is asked to enter the name, age and salary of a Employee inside main() function.

Then, the structure variable p is to passed to a function using.

printData(p);

The return type of printData() is void and a single argument of type structure Employee is passed.

Then the members of structure p is displayed from this function.


Example 2: C++ Returning structure from function

#include <iostream>
using namespace std;
struct Employee {
    char name[50];
    int age;
    float salary;
};
Employee getData(Employee); 
void printData(Employee); 

int main(){
    Employee p;
    p = getData(p);   
    printData(p);

    return 0;
}

Employee getData(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;
}

Output

Enter Full name: Micheal Clark
Enter age: 20
Enter salary: 20000
                                                                      
Displaying Information.
Name: Micheal Clark
Age: 20
Salary: 20000

The output of this program is same as program above.

In this program, the structure variable p of type structure Employee is defined under main() function.

The structure variable p is passed to getData() function which takes input from user which is then returned to main function.

p = getData(p); 

Note: The value of all members of a structure variable can be assigned to another structure using assignment operator = if both structure variables are of same type. You don't need to manually assign each members.

Then the structure variable p is passed to printData() function, which displays the information.


2. Call by reference

In this method of passing the structures to function, 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.

Consider this example:


Example 3: C++ Structure and Function

#include <iostream>
using namespace std;
struct Employee{
    char name[50];
    int age;
    float salary;
};
void readData(Employee &);   // Function declaration
void printData(Employee);   // Function declaration
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: Micheal Clark
Enter age: 20
Enter salary: 20000
                                                                      
Displaying Information.
Name: Micheal Clark
Age: 20
Salary: 20000

The output of this program is same as program above.

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


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Structure & Functions in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Pointers to structure.

- Related Topics