Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Structure and Function

In this tutorial, you’ll find relevant examples to pass structures as an argument to a function, and use them in your program with the help of examples.


Passing structure to function

  • A Structure variables can be passed to a function and returned in a similar way as normal arguments.
  • A structure can be passed to any function from main() function or from any sub function.
  • Structure definition will be available within the function only.
  • It won’t be available to other functions unless it is passed to those functions by value or by address (reference).

Ways to pass structure

If the structure itself is an argument, then it is called “call by value”. If the reference of the structure is passed as an argument then it is called “call by reference”.

  1. Call by value
  2. Call by reference

1. Call by value

When a structure is passed as argument to a function using call by value method, any change made to the contents of the structure variable inside the function to which it is passed do not affect the structure variable used as an argument.

Consider this example:


Example 1: C++ Structure and Function

#include <iostream>
using namespace std;
struct Employee{
    char name[50];
    int age;
    float salary;
};
void printData(Employee);   // Function declaration
int main(){
    Employee 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
    printData(p);                                                                
    return 0;
}
void printData(Employee p){
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout <<"Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}

Output

Enter Full name: Micheal Clark
Enter age: 20
Enter salary: 20000
                                                                      
Displaying Information.
Name: Micheal Clark
Age: 20
Salary: 20000

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

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

printData(p);

The return type of printData() is void and a single argument of type structure Employee is passed.

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


Example 2: C++ Returning structure from function

#include <iostream>
using namespace std;
struct Employee {
    char name[50];
    int age;
    float salary;
};
Employee getData(Employee); 
void printData(Employee); 

int main(){
    Employee p;
    p = getData(p);   
    printData(p);

    return 0;
}

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

Output

Enter Full name: Micheal Clark
Enter age: 20
Enter salary: 20000
                                                                      
Displaying Information.
Name: Micheal Clark
Age: 20
Salary: 20000

The output of this program is same as program above.

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

The structure variable p is passed to getData() function which takes input from user which is then returned to main function.

p = getData(p); 

Note: The value of all members of a structure variable can be assigned to another structure using assignment operator = if both structure variables are of same type. You don’t need to manually assign each members.

Then the structure variable p is passed to printData() function, which displays the information.


2. Call by reference

In this method of passing the structures to function, the address of a structure variable/object is passed to the function using address of (&) operator. So any change made to the contents of structure variable inside the function are reflected back to the calling function.

Consider this example:


Example 3: C++ Structure and Function

#include <iostream>
using namespace std;
struct Employee{
    char name[50];
    int age;
    float salary;
};
void readData(Employee &);   // Function declaration
void printData(Employee);   // Function declaration
int main(){
    Employee p;
    readData(p);
    printData(p);
    return 0;
}

void readData(Employee &p){
    cout << "Enter Full name: ";
    cin.get(p.name, 50);
    cout << "Enter age: ";
    cin >> p.age;
    cout << "Enter salary: ";
    cin >> p.salary;
}
void printData(Employee p){
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout <<"Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}

Output

Enter Full name: Micheal Clark
Enter age: 20
Enter salary: 20000
                                                                      
Displaying Information.
Name: Micheal Clark
Age: 20
Salary: 20000

The output of this program is same as program above.

Structures are usually passed by reference method because it saves the memory space and executes faster.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!