C++ structures and functions
In C++, structures are user-defined data types that allow you to combine different types of variables into a single unit. A function is a block of code that performs a specific task. Structures and functions can work together in C++ to organize code better, simplify problem-solving, and make programs more modular and reusable.
A structure in C++ is a composite data type that groups variables of different types under a single name. The members of a structure can be of any data type, and each member is called a data member.
struct StructureName {
data_type member1;
data_type member2;
// Other members
};
StructureName
: The name of the structure.member1
, member2
, etc.: The variables (data members) inside the structure.
#include <iostream>
using namespace std;
// Define a structure to store a person's details
struct Person {
string name;
int age;
float height;
};
int main() {
// Create a structure variable
Person person1;
// Assign values to the structure members
person1.name = "Alice";
person1.age = 30;
person1.height = 5.6;
// Access and print the structure members
cout << "Name: " << person1.name << endl;
cout << "Age: " << person1.age << endl;
cout << "Height: " << person1.height << endl;
return 0;
}
Output:
Name: Alice
Age: 30
Height: 5.6
Explanation:
Person
is defined with three members: name
, age
, and height
.person1
is created, and its members are assigned values..
) and print them.A function in C++ is a block of code designed to perform a specific task. Functions improve code organization and reusability. Functions can take input parameters and return output values.
return_type function_name(parameter_list) {
// Function body
return value; // (if return type is not void)
}
return_type
: The type of value the function will return (e.g., int
, float
, void
if it does not return anything).function_name
: The name of the function.parameter_list
: The parameters (input) the function accepts, if any.
#include <iostream>
using namespace std;
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
int main() {
int num1 = 10, num2 = 20;
// Call the function and print the result
cout << "Sum: " << add(num1, num2) << endl;
return 0;
}
Output:
Sum: 30
Explanation:
add
takes two integer parameters and returns their sum.main
function, we call add(num1, num2)
and print the result.Structures can be passed to functions to manipulate their members or perform operations on the data they hold. You can pass a structure to a function in two ways:
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
// Function to modify structure data (pass by value)
void modifyPerson(Person p) {
p.age = 35; // This change will not affect the original structure
cout << "Modified Age in function: " << p.age << endl;
}
int main() {
Person person1 = {"Alice", 30};
cout << "Original Age: " << person1.age << endl;
// Passing the structure by value
modifyPerson(person1);
cout << "Age after function call: " << person1.age << endl; // Will not be changed
return 0;
}
Output:
Original Age: 30
Modified Age in function: 35
Age after function call: 30
Explanation:
modifyPerson
function receives a copy of the person1
structure.age
inside the function does not change the original structure because the structure was passed by value.
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
// Function to modify structure data (pass by reference)
void modifyPerson(Person& p) {
p.age = 35; // This change will affect the original structure
}
int main() {
Person person1 = {"Alice", 30};
cout << "Original Age: " << person1.age << endl;
// Passing the structure by reference
modifyPerson(person1);
cout << "Age after function call: " << person1.age << endl; // Will be changed
return 0;
}
Output:
Original Age: 30
Age after function call: 35
Explanation:
modifyPerson
function receives the structure by reference (Person& p
).age
inside the function directly changes the original structure since we passed it by reference.A function can return a structure to the caller. In this case, you should ensure that the structure returned is valid. It's important to note that structures are returned by value unless explicitly returned by reference.
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
// Function that returns a structure
Person createPerson(string name, int age) {
Person p;
p.name = name;
p.age = age;
return p; // Returning the structure
}
int main() {
Person person1 = createPerson("Alice", 30);
cout << "Name: " << person1.name << endl;
cout << "Age: " << person1.age << endl;
return 0;
}
Output:
Name: Alice
Age: 30
Explanation:
createPerson
function creates and returns a structure of type Person
.person1
variable and print its members.