Pass Array to Functions in C++
Last Updated :
03 Jan, 2024
In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code.
In C++, an array can be passed in a function using a pointer or reference. Understanding the different approaches to pass arrays is important for writing code according to the needs.
Methods to Pass Array to a Function in C++
In C++, we have the following ways to pass an array as a parameter to the function:
- As a sized array
- As an unsized array
- As a pointer (pass by pointer)
- As a reference (pass by reference)
1. Passing as a Sized Array
In this method, we pass the array in the same way we declare it with the array type, name, and size. As we can see, we still have to pass the size of the array as another parameter because at the end, the array will be treated as a pointer in the function.
Syntax
return_type function_name (datatype array_name [size], int size)
Example
The below example demonstrates the passing of array as sized array.
C++
// C++ program to demonstrate the passing of array as sized
// array.
#include <iostream>
using namespace std;
// function to update array elements
void printarray(int a[10])
{
for (int i = 0; i < 5; i++)
a[i] = a[i] + 5;
}
int main()
{
// array declaration
int a[5] = { 1, 2, 3, 4, 5 };
printarray(a); // Passing array to function
// printing array elements
for (int i = 0; i < 5; i++)
cout << a[i] << " ";
return 0;
}
2. Passing as an Unsized Array
This method is similar to the previous method, but the difference is that we dont specify the size of the array.
Syntax
return_type function_name (data_type array_name[])
Example
The below example demonstrates the passing of array as unsized array.
C++
// C++ program to demonstrate the passing of array as
// unsized array.
#include <iostream>
using namespace std;
// function to update array elements
void printarray(int a[],int size)
{
for (int i = 0; i < size; i++)
a[i] = a[i] + 5;
}
int main()
{
// array creation
int a[5] = { 1, 2, 3, 4, 5 };
int n=5;
printarray(a,n); // Passing array to function
// printing array elements
for (int i = 0; i < n; i++)
cout << a[i] << " ";
return 0;
}
3. Passing Array as a Pointer
In this method, we pass the memory address of the first element of the array. This method also allows for dynamic array sizes.
Syntax
return_type function_name (datatype *array_name)
Example
The below example demonstrates how to pass array as a pointer to function.
C++
// C++ program to demonstratethe passing of array by
// pointer.
#include <iostream>
using namespace std;
// function to print array elements
void printarray(int* a)
{
for (int i = 0; i < 5; i++)
*(a + i) = *(a + i) + 5;
}
int main()
{
// array creation
int a[5] = { 1, 2, 3, 4, 5 };
printarray(a); // Passing array to function
for (int i = 0; i < 5; i++)
cout << a[i] << " ";
return 0;
}
4. Passing Array as a Reference
In this method, we pass an array to a function as a reference and also explicitly pass its size i.e. we are passing both the reference to the first element and the size of the array.
Syntax
return_type function_name(data_type (&arr)[size])
Example
C++
// C++ to demonstrate array passing by pass by reference
#include <iostream>
using namespace std;
void modifyArray(int (&arr)[5])
{
// deducing size
int size = sizeof(arr) / sizeof(int);
for (int i = 0; i < size; ++i) {
arr[i] *= 2;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = 5;
modifyArray(arr);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Similar Reads
Passing Vector to a Function in C++ To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.Table of ContentPass Vector by ValuePass
5 min read
Passing Pointers to Functions In C++ Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any varia
4 min read
std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
C++ Functions - Pass By Reference In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so
3 min read
Functions in C++ A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
9 min read
list assign() function in C++ STL The list::assign() is a built-in function in C++ STL which is used to assign values to a list. It can also be used to copy elements from one list to another. To assign elements to a list. Syntax: list_name.assign(count, value) Parameters: This function accepts two mandatory parameters as shown in th
2 min read
Function Pointer in C++ Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
C++ Map of Functions Prerequisites: Functions in C++Map in C++ Maps in C++ are a very useful data structure for storing key-value pairs. A map is the best way to store where we can directly find and access the value using a key with the best time complexity. However, did you know that you can also store functions as val
5 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
How to create a function in MATLAB ? A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read