fastread homefastrread library fastread menu

C++ : Structure and Function

Tutorial by:Manisha Dubey      Date: 2016-06-18 00:45:33

❰ Previous Next ❱

Structure variables can be passed to a function and returned from a function in similar way as normal arguments:

Passing structure to function in C++

A structure variable can be passed to a function in similar way as normal argument. Consider this example:

Example 1: C++ Structure and Function

#include <iostream>
using namespace std;

struct  person {
    char name[50];
    int age;
    float salary;
};

void displayData(person);   // Function declaration

int main() {

    person p;

    cout << "Enter Full name: ";
    cin.get(p.name, 50);
    cout << "Enter age: ";
    cin >> p.age;
    cout << "Enter salary: ";
    cin >> p.salary;

    displayData(p);    // Function call with structure variable as arugment

    return 0;
}

void displayData(person p1) {

    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;
    
}

Output

Enter Full name: Bill Jobs
Enter age: 55
Enter salary: 34233.4

Displaying Information.
Name: Bill Jobs
Age: 55
Salary: 34233.4

In this program, user is asked to enter the data for the members of a structure variable inside main() function. Then that structure variable to passed to a function using code.

displayData(p);

The return type of displayData() is void and one argument of type structure person is passed. The function prototype also should match the function definition. Then the members of structure p1 is displayed from this function.

Returning structure from function in C++

#include <iostream>
using namespace std;

struct  person {
    char name[50];
    int age;
    float salary;
};

person getData(person); 
void displayData(person); 

int main() {

    person p;

    p = getData(p);   
    displayData(p);

    return 0;
}

person getData(person p1) {

    cout << "Enter Full name: ";
    cin.get(p1.name, 50);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;
    return p1;

}

void displayData(person p1) {

    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;
    
}

 

 
 

The output of this program is same as program above.

In this program, the structure variable p of type structure person is defined under main() function. The structure variable p is passed to getData() function which takes input from user and that value is stored in member function of structure variable p1 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 displayData() function, which displays the information.

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website