Skip to main content

Arrays/Vectors

Passing Array in Functions

Hello Coders!

We already studied in the last article about how to pass Arrays in functions. But do you know that it's not the only way to do that! In this article, we are going to see multiple ways to pass an array into functions:

  1. Pass as a pointer
  2. Array as a parameter

Passing array as a pointer

Remember, how we passed variables as pointers in functions? In the same way, let's pass the arrays in functions too!

To understand how pointers work, let's take a hypothetical situation where you want to convey the datasheet of your subject exams to your friend that you saw pinned on the noticeboard.

One way is that you note all the dates and subjects on a paper and give it to him. Another way is to tell him the location of that notice board so that he can go and check himself.

This method is like the second way in this example. We pass the memory address of the first element of the array. This method also allows for dynamic array sizes.

The benefit of passing arrays as pointers

Suppose you see the datasheet with 4 subjects and note it down. Afterwards, a subject's exam date is changed.

In the first way where you write it on paper, the changes won't be reflected as your information will be obsolete (outdated). This problem is eliminated in the current method because your friend will directly be accessing the notice board (memory address of the array) unlike the copied array.

Let's understand this method with an example.

Example

Write a C++ program that prints the second element (1st index) of the array after modifying it in a func function.

Input

arr[4] = {3, 7, 12, 5};
x = 17

Output

Value is 17

Solution

Try yourself

#include <iostream>
using namespace std;

// Function to find the sum of the 1st and 4th elements
int func(int* brr, int size, int x) {
   brr[1] = x;
}

int main() {
    int arr[4] = {3, 7, 12, 5};

    // Calculate the size of the array
    int size = sizeof(arr) / sizeof(arr[0]);
    
    // Input x that will store the new value of arr[1]
    int x;  
    cin >> x;

    // Call modifying function func
    func(arr, size, x);

    // Print arr[1]
    cout<<arr[1];

    return 0;
}

Try on compiler

As you can see, the modifications made in the function are reflected while printing just like in case of passing variables as pointers as parameters.


Passing array as parameter

In the previous method, we passed the array as a pointer into the modification function. But what if I directly pass the array in the function? Will the changes be reflected like in the previous method? Or will it be just passed like value as in the case of variables? Let's see an example and solve the quest 🕵️

Example

Write a C++ program that prints the second element (1st index) of the array after modifying it in a func function.

Input

arr[4] = {3, 7, 12, 5};
x = 17

Output

Value is 17

Solution

Try yourself

#include <iostream>
using namespace std;

// Function to find the sum of the 1st and 4th elements
int func(int brr[], int size, int x) {
   brr[1] = x;
}

int main() {
    int arr[4] = {3, 7, 12, 5};

    // Calculate the size of the array
    int size = sizeof(arr) / sizeof(arr[0]);
    
    // Input x that will store the new value of arr[1]
    int x;  
    cin >> x;

    // Call modifying function func
    func(arr, size, x);

    // Print arr[1]
    cout<<arr[1];

    return 0;
}

Try on compiler

Voila! The value is changed implying that arrays are never passed as value by default as in the case of variables. But how are they passed then?

As pointers!

Array introductory articles cover this concept in detail but to sum it up, Arrays by default are passed as pointers, i.e., the memory address of the first element is passed in the function. Any operation performed over the array element is by accessing them using the address itself rather than the values.


With this, we come to the end of all our array concepts. But is this over? No! It's time to solve some problems now. So get ready and polish all the concepts. Meet you in the next article 👋