Broad Network


Passing an Array or Container to a Function in C++

Some Features of C++ Entities – Part 6

Forward: In this part of the series, I show you how to pass an array or container to a function.

By: Chrysanthus Date Published: 26 Aug 2012

Introduction

This is part 6 of my series, Some Features of C++ Entities. In this part of the series, I show you how to pass an array or container to a function. Remember, in this series, I explain C++ features that are not commonly found in C++ tutorials. I also advice you to study C++ in my volume, C++ Tutorials, in the order in which the tutorials have been given. To access the volume online, just type “C++ Tutorials” without the quotes in the search box of this page and click Search.

Passing a Fundamental Object to a Function
The following program shows how to pass an int to a function:

#include <iostream>
using namespace std;

void fn(int iden)
    {
        cout << iden;
    }

int main()
    {
        fn(33);

        return 0;
    }

Passing an Array to a Function
You pass an array in a similar way, but there are certain peculiarities you have to know with the array. The array parameter in the function interface has the square brackets without the number for the size of the array; that is, these square brackets are empty. In the calling function, you write only the name of the array as argument without the square brackets and without any number. The following program illustrates this (read and try it):

#include <iostream>
using namespace std;

int myArr[15];

void fn(int anArr[])
    {
        anArr[3] = 44;
        cout << anArr[3] << "\n";
        cout << myArr[3] << "\n";
    }

int main()
    {
        fn(myArr);

        return 0;
    }

The above rules apply to arrays declared with the initializer as well. The following program illustrates this (read and try it):

#include <iostream>
using namespace std;

int marks[] = {43, 29, 35, 50, 60, 65, 78, 56, 67, 90};

void fn(int anArr[])
    {
        cout << anArr[3] << "\n";
        cout << marks[3] << "\n";
    }

int main()
    {
        fn(marks);

        return 0;
    }

Function Returning an Array
A function cannot return an array created inside a function definition, no matter the way the array was created (by initializer or not). The arrays in the above two programs have been created outside the function (definition); so do not use them as they are to test the returning of an array.

Passing a Container to a Function
To pass a container there is no restriction. You can pass a container as value or as reference. A function can return a container. Before I give you an example, just remember how to declare a container, when the declaration is alone or when it is a parameter in the function interface. A simple vector declaration example is:

    vector<int> myVtor;

With a container the maximum size and the square brackets are not used when passing as a parameter. However, in the function definition, you can use the square brackets as expected. The following example shows how to pass a container (vector) to a function by value (read and try it):

#include <iostream>
#include <vector>
using namespace std;

vector<int> myVtor(15);

void fn(vector<int> aVtor)
    {
        aVtor[3] = 66;
        cout << aVtor[3] << "\n";
        cout << myVtor[3] << "\n";
    }

int main()
    {
        fn(myVtor);

        return 0;
    }

Note the way the vector has been declared as a parameter in the function interface: you begin with the word, vector, then object type in angle brackets; all that forms the container type; then you write the name of the container.

In the function interface, you declare the container without any size information or brackets and in the calling function, you type the argument without any size information or brackets.

A Note on Passing Arrays and Vectors
There is only one way to pass an array: you cannot say you would pass an array by value or by reference (pointer). Passing an array means passing an object by pointer. So whatever name you give to the array in the declaration in the function interface, you are still referring to the same array in memory. On the other hand, when you pass a container by value and the parameter name in the function interface is different, then you will end up with 2 copies of the container of your program in memory. If you do not want 2 copies, then pass by reference.

Conclusion
You can pass an array or container to a function. For the array, in the function interface, you type an empty square bracket pair. For the container, in the function interface, you type just the name you want to be used in the function definition as the container. For the array and container and in the function call, you type just the name as argument (no square brackets, no number, nothing).

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message