Array and Functions in C++

Comprehensive study notes, diagrams, and exam preparation for Array and Functions in C++.

Array and Functions in C++

Definition

An array is a collection of elements of the same data type stored in contiguous memory locations and accessed using a common name with an index.

A function is a named block of code that performs a specific task and can be called whenever needed, with or without arguments, and may return a value.

In C++, arrays and functions are closely connected because arrays are often passed to functions for processing, searching, sorting, and analysis.


Main Content

1. Arrays in C++

Meaning and Structure

An array stores multiple values of the same type under one name. Each item in the array is called an element, and each element is identified by an index starting from 0. For example, if we declare int marks[5];, then marks[0] refers to the first element and marks[4] refers to the last element.

Types and Use Cases

Arrays may be one-dimensional, two-dimensional, or multidimensional. A one-dimensional array is used for linear data such as a list of marks. A two-dimensional array is used for tabular data such as a matrix or student records. Arrays are widely used in loops, searching, sorting, and statistical calculations.

Example:

#include <iostream>
using namespace std;

int main() {
    int marks[5] = {78, 85, 90, 66, 88};
    for(int i = 0; i < 5; i++) {
        cout << marks[i] << " ";
    }
    return 0;
}

Here, the loop prints all five elements of the array.


2. Functions in C++

Purpose and Reusability

Functions break a large program into smaller logical units. A function can perform a task such as adding numbers, displaying data, or finding the largest element in an array. This improves code reusability because the same function can be called multiple times without rewriting code.

Components of a Function

A function generally includes a return type, function name, parameter list, and body. Functions can be of different types such as built-in functions, user-defined functions, recursive functions, and inline functions. A function may return a value using return, or it may be a void function if no value is returned.

Example:

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << add(10, 20);
    return 0;
}

This function add takes two integers and returns their sum.


3. Arrays and Functions Together

Passing Arrays to Functions

Arrays are commonly passed to functions so that operations can be performed on them. When an array is passed to a function, the function receives the address of the first element. This allows the function to access and modify the original array values.

Common Operations Using Functions

Functions can be used to calculate the sum of elements, search for an element, find the maximum or minimum, reverse the array, and sort the elements. Passing arrays to functions makes programs modular and easier to understand.

Example:

#include <iostream>
using namespace std;

void display(int arr[], int size) {
    for(int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
}

int main() {
    int nums[4] = {10, 20, 30, 40};
    display(nums, 4);
    return 0;
}

In this example, the display function prints all array elements.


Working / Process

1. Declare and initialize the array

Create an array of required size and assign values either at the time of declaration or later using input from the user.

2. Define the function

Write a function that accepts the array and its size as parameters. Inside the function, perform the required task such as summing, searching, or displaying elements.

3. Call the function from main()

Pass the array name and size to the function. The function processes the data and may return a result or directly display output.


Advantages / Applications

Saves memory and organizes data efficiently

Arrays store many values of the same type in a compact and systematic way.

Improves code modularity and reusability

Functions allow tasks to be written once and reused multiple times, reducing duplication.

Useful in many practical problems

Arrays and functions are used in searching, sorting, student record systems, matrix operations, data analysis, and many real-life software applications.


Summary

Arrays store multiple values of the same type in a single structure, while functions divide programs into reusable blocks. In C++, these two concepts are often used together to process data efficiently. Understanding arrays and functions is essential for writing organized and effective programs in Unit II.