Structure and Function in C++
C ++ – Structure and Function
To pass a struct variable as an argument to a function in C++, you need to include the name of the structure type in the function prototype and the function call. This page is all about structure and function in C++. In C++, a structure is a user-defined data type that groups together related data elements and allows them to be treated as a single entity.Structures are often used to represent real-world objects, such as a point in 2D space, a customer record in a database, or a complex number.
Structure and Function in C++
In C++, you can define functions inside a structure as member functions. These functions have access to the member variables of the structure and can be used to manipulate them.
Here’s an example of a structure with member functions in C++:
#include <iostream> #include <bits/stdc++.h> using namespace std; struct Employee { string name; int age; float salary; // Define a member function that prints the employee's details void printDetails() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Salary: " << salary << endl; } }; int main() { Employee emp = {"John Smith", 30, 45000.0}; // Call the member function to print the employee's details emp.printDetails(); return 0; }
Output
Name: John Smith Age: 30 Salary: 45000
There are several ways to pass structure members to functions in C++. Here are two common approaches:
Pass the structure as an argument to the function:
#include <iostream> #include <bits/stdc++.h> using namespace std; struct Point { int x; int y; }; void printPoint(struct Point p) { cout << "\nDisplaying coordinates of a point." << endl; cout << "(" << p.x << " , " ; cout << p.y << ")"; } int main() { struct Point p = {1, 2}; printPoint(p); return 0; }
Output
Displaying coordinates of a point. (1 , 2)In this example, the
printPoint
function takes a struct Point
argument, which allows it to access the x
and y
members of the structure. Return struct from a function
To return a struct from a function in C++, you can simply use the return statement and specify the struct as the return value. Here’s an example:
#include <iostream> #include <bits/stdc++.h> using namespace std; struct Point { int x; int y; }; struct Point createPoint(int x, int y) { struct Point p = {x, y}; return p; } int main() { struct Point p = createPoint(1, 2); cout << "\nDisplaying coordinates of a point." << endl; cout << "(" << p.x << " , " ; cout << p.y << ")"; return 0; }
Output
Displaying coordinates of a point. (1 , 2)In this example, the
createPoint
function takes two int
arguments and returns a struct Point
value that is initialized with those values. The returned struct is then assigned to a local variable p
in the main
function, which is then printed to the console. Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment