Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to pass or return a structure to/from a Function in C/C++?
Next article icon

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  

Next Article
How to pass or return a structure to/from a Function in C/C++?

V

vinaychopra92vc
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences