Structures and Functions in c++
Structures and Functions in c++
n this article, you'll find relevant examples to pass structures as an argument to a function, and use them in your program.
Structure variables can be passed to a function and returned in a similar way as normal arguments.
n this article, you'll find relevant examples to pass structures as an argument to a function, and use them in your program.
Structure variables can be passed to a function and returned in a 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;
// 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: Bill Jobs Enter age: 55 Enter salary: 34233.4 Displaying Information. Name: Bill Jobs Age: 55 Salary: 34233.4
Comments
Post a Comment