How to pass or return a structure to/from a Function in C/C++? Last Updated : 16 Dec, 2019 Comments Improve Suggest changes Like Article Like Report A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. How to pass structure as an argument to the functions? Passing of structure to the function can be done in two ways: By passing all the elements to the function individually. By passing the entire structure to the function. In this article, entire structure is passed to the function. This can be done using call by reference as well as call by value method. Examples 1: Using Call By Value Method CPP // C++ program to pass structure as an argument // to the functions using Call By Value Method #include <bits/stdc++.h> using namespace std; struct Distance { int kilometer; int meter; }; // accepts distance as its parameters void TotalDistance(Distance d1, Distance d2) { // creating a new instance of the structure Distance d; // assigning value to new instance of structure d.kilometer = d1.kilometer + d2.kilometer + (d1.meter + d2.meter) / 1000; d.meter = (d1.meter + d2.meter) % 1000; cout << "Total distance:"; cout << "kilometer: " << d.kilometer << endl; cout << "meter: " << d.meter << endl; } // Function that initialises the value // and calls TotalDistance function void initializeFunction() { // creating two instances of Distance Distance Distance1, Distance2; // assigning values to structure elements Distance1.kilometer = 10; Distance1.meter = 455; Distance2.kilometer = 9; Distance2.meter = 745; // calling function with (structure) // distance as parameters TotalDistance(Distance1, Distance2); } // Driver code0 int main() { // Calling function to do required task initializeFunction(); return 0; } Output: Total distance:kilometer: 20 meter: 200 Examples 2: Using Call By Reference Method CPP // C++ program to pass structure as an argument // to the functions using Call By Reference Method #include <bits/stdc++.h> using namespace std; struct number { int n; }; // Accepts structure as an argument // using call by reference method void increment(number& n2) { n2.n++; } void initializeFunction() { number n1; // assigning value to n n1.n = 10; cout << " number before calling " << "increment function:" << n1.n << endl; // calling increment function increment(n1); cout << "number after calling" << " increment function:" << n1.n; } // Driver code int main() { // Calling function to do required task initializeFunction(); return 0; } Output: number before calling increment function:10 number after calling increment function:11 How to return a structure from the functions? To return a structure from a function the return type should be a structure only. Examples: CPP // C++ program to return a structure from // a function using Call By Value Method #include <iostream> #include <stdlib.h> using namespace std; // required structure struct Employee { int Id; string Name; }; // return type of the function is structure Employee data(Employee E) { // Assigning the values to elements E.Id = 45; E.Name = "aman"; // returning structure return (E); } // Driver code int main() { // creating object of Employee Employee Emp; // calling function data to assign value Emp = data(Emp); // display the output cout << "Employee Id: " << Emp.Id; cout << "\nEmployee Name: " << Emp.Name; return 0; } Output: Employee Id: 45 Employee Name: aman Comment More infoAdvertise with us Next Article How to pass or return a structure to/from a Function in C/C++? V vinaychopra92vc Follow Improve Article Tags : C++ cpp-structure C-Structure & Union Practice Tags : CPP Similar Reads How to Return a Local Array From a C++ Function? Here, we will build a C++ program to return a local array from a function. And will come across the right way of returning an array from a function using 3 approaches i.e. Using Dynamically Allocated ArrayUsing Static Array Using Struct C++ // C++ Program to Return a Local // Array from a function W 3 min read How to return local variables from a function in C++ In C++, returning a local variable from a function may result in an error due to the deallocation of variable memory after some value is returned from the function. But there are some ways using which we can safely return the local variables or the address of local variables and manipulate the data 4 min read Return From Void Functions in C++ Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but the 2 min read Passing Vector to a Function in C++ To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.Table of ContentPass Vector by ValuePass 5 min read std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++ There can be some instances where you need to return multiple values (maybe of different data types ) while solving a problem. One method to do the same is by using pointers, structures or global variables, already discussed here There is another interesting method to do the same without using the a 2 min read Like