C++ Structure and Functions

C++ Structure and Functions

4 mins read2.7K Views Comment
Updated on Jan 16, 2023 11:55 IST

In C++, the structure is known to be a user-defined data structure and function is defined as a selection of code that only executes when called. In this article, we will discuss C++ structure and function.

2022_11_MicrosoftTeams-image-66.jpg

We frequently encounter situations while programming where we must store a collection of data, whether they are of related or unrelated data kinds. In C++, we have arrays that are used to store collections of data of related data types in adjacent memory addresses, whereas arrays, user-defined data types are called Structures in C++ and are used to store collections of objects with dissimilar data types. In this article, we will talk about structures and functions in C++ along with the need to declare both structure and functions while programming in C++. We will discuss the method for their declaration and a few examples associated with it.

Structures in C++

In C++, the structure is known to be a user-defined data type. When objects of potentially diverse types are grouped together into one type, a structure provides a data type that may be utilized for this.

Creation of a C++ Structure

To create a structure, use the ‘struct’ keyword. The following is a general syntax for building structures:


 
struct structName{
mem1;
mem2;
mem3;
.
.
.
memN;
}
Copy code

In C++, structures can have two different kinds of members:

Data Members: These are the data elements are typical C++ variables. In C++, we can build a structure using variables of various data kinds.

Member Functions: These members’ functions are typical C++ functions. The user can also include functions in a structure declaration in addition to variables.

Let us see an example in C++ to see the declaration f structures using dot operator:


 
#include
using namespace std;
struct Declare {
int a, b;
};
int main()
{
struct Declare x = { 0, 1 };
x.a = 20;
cout << "a = " << x.a << ", b = " << x.b;
return 0;
Copy code

The output of the C++ code will be:

a = 20, b = 1

Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
– / –
1 hours
Free
2 hours
Free
1 hours
Free
1 hours

Function in C++

A function is defined as a section of code that only executes when called.

You can supply parameters—data—to a function.

Functions are used to carry out certain activities and are crucial for code reuse: Define the code once and use it many times.

Creation of a C++ Function

Some pre-defined functions are available in C++, like main(), which is used to run programs. However, you can also design your own custom functions to carry out specific tasks.

Put parentheses after the function name to construct a function (often referred to as declare).


 
void Function() {
//write the code that is to be executed
}
Copy code

Passing Structure to Function

  • Similar to regular arguments, structure variables can be supplied to a function and returned.
  • From the main() method or any sub-function, a structure could be provided to any other function.
  • Only the function will have access to the structure definition.
  • Other functions won’t be able to access it unless it is supplied to them by value or by address.

Let us see the C++ code below to understand this process better:


 
#include
using namespace std;
struct Employee {
char name[50];
int age;
float salary;
};
void displayData(Employee);
int main() {
Employee a;
cout << "Enter your full name: ";
cin.get(a.name, 60);
cout << "age: ";
cin >> a.age;
cout << "salary: ";
cin >> a.salary;
displayData(a);
return 0;
}
void displayData(Employee a) {
cout << "\nDisplaying the employee's Information." << endl;
cout << "Name: " << a.name << endl;
cout <<"Age: " << a.age << endl;
cout << "Salary: " << a.salary;
}
Copy code

Output

Enter your full name: Megha Garg

age: 27

salary: 10000

Displaying the employee’s Information.

Name: Megha Garg

Age: 27

Salary: 10000

In the C++ code above, the employee is requested to enter his full name, age, and salary  inside the main() function. Then, a function is called and it receives the structural variable a. The function call with structure variable as an argument is passed using:

displayData(p);

One argument of type structure Employee is passed, and the return statement of displayData() is void. The members of structure a then are shown using this function.

Let us see another C++ code below to understand the return of structure from a function.


 
#include
using namespace std;
struct Employee {
char name[60];
int age;
float salary;
};
Employee getData(Employee);
void displayData(Employee);
int main() {
Employee a, temp;
temp = getData(a);
a = temp;
displayData(a);
return 0;
}
Employee getData(Employee a) {
cout << "Enter your full name: ";
cin.get(a.name, 50);
cout << "age: ";
cin >> a.age;
cout << "salary: ";
cin >> a.salary;
return a;
}
void displayData(Employee a) {
cout << "\n Displaying the Employee's Information:" << endl;
cout << "Name: " << a.name << endl;
cout <<"Age: " << a.age << endl;
cout << "Salary: " << a.salary;
}
Copy code

Output

Enter your full name: Megha Garg

age: 27

salary: 10000

Displaying the Employee’s Information:

Name: Megha Garg

Age: 27

Salary: 10000

In the above C++ code, Under the main() function, we have created two structural variables of type Employee in this programme: a and temp.

The getData() function receives the structural variable p and uses it to gather user input, which is subsequently saved in the temp variable.

Once temp = getData(a), a is given the value of temp.

The structural variable a is then supplied to the displayData() function, which shows the data, after setting it to temp.

Ways to Pass Structure

There are two main ways for passing a structure to function. Let us have a look at the section below to understand it in a better manner.

Class by Value

Any changes made to the structure variable’s contents inside the function to which it is supplied have no effect on the structure variable used as an argument when a structure is passed as an argument using the call by value method.

Call by Reference

The value of a structure variable or object is supplied to the function that uses the address of (&) operator in this way of providing structures to functions. Therefore, any modifications made to the structural variable’s contents within the function are transmitted back to the caller function.

Important points to know about structure and function:

  • Declarative initialization of structure members is possible in C++. For instance, the following C++ program runs without any errors and completes successfully.
  • Declarations cannot initialize structure members.
  • The only way to access structures is by using the dot operator(.)

Author: Megha Chadha

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio