C++ structures and functions


Introduction

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.


1. What is a Structure in C++?

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.

Syntax for Defining a Structure:

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.
  • The structure defines a blueprint for creating variables of that type.

Example: Defining and Using a 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:

  • The structure Person is defined with three members: name, age, and height.
  • A structure variable person1 is created, and its members are assigned values.
  • We access the members using the dot operator (.) and print them.

2. What is a Function in C++?

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.

Syntax for Defining a Function:

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.

Example: Defining and Using a Function

#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:

  • The function add takes two integer parameters and returns their sum.
  • In the main function, we call add(num1, num2) and print the result.

3. Passing Structures to Functions

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:

  • By value: The function gets a copy of the structure, and changes made inside the function do not affect the original structure.
  • By reference: The function gets the actual structure, and changes made inside the function will affect the original structure.

Example: Passing a Structure by Value

#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:

  • The modifyPerson function receives a copy of the person1 structure.
  • Modifying the age inside the function does not change the original structure because the structure was passed by value.

Example: Passing a Structure by Reference

#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:

  • The modifyPerson function receives the structure by reference (Person& p).
  • Modifying the age inside the function directly changes the original structure since we passed it by reference.

4. Returning Structures from Functions

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.

Example: Returning a Structure from a Function

#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:

  • The createPerson function creates and returns a structure of type Person.
  • We store the returned structure in the person1 variable and print its members.