C++ Structure and Function

C++ Structure and Function

C++ Structure and Function

In this article, you’ll find relevant examples to pass structures as an argument to capacity, and use them in your program.

Structure variables can be passed to a function and returned likewise as typical contentions.

Passing structure to work in C++

A structure variable can be passed to a capacity in comparable manner as typical contention. Think about 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;

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

    return 0;
}

void displayData(Person p)
{
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout <<"Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}

Output

Enter Full name: sohail 
Enter age: 50
Enter salary: 34233.4

Displaying Information.
Name: sohail 
Age: 50
Salary: 34233.4

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

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

displayData(p);

The return type of displayData() is void and a single argument of type structure Person is passed.

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


Example 2: 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 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 displayData(Person p)
{
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout <<"Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}

The yield of this program is same as program above.

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

The structure variable p is passed to getData() work which takes input from the user which is then coming back to primary capacity.

p = getData(p); 

Note: The value of all members from a structure variable can be relegated to another structure using assignment operator = if both structure factors are of same kind. You don’t have to manually relegate every members.

At that point, the structure variable p is passed to the displaydata() function, which shows the information.


Please feel free to give your comment if you face any difficulty here.

Leave a Reply

Your email address will not be published. Required fields are marked *