Functions And Array

Functions And Array

A function in C++ is a self-contained block of code that performs a specific task. Think of it as a machine: you input something, the function processes it, and then it outputs the result. Functions make your code reusable and organized.

An array is defined as a collection of items stored at contiguous memory locations under the same name. Ex: int (a,b,c,d,e ) can be grouped in a single variable as int a[5]. Now five continuous memory location are assigned with the same name ‘a’. Instead of creating separate variables to store data, it is an efficient organization to store data  in a single variable called array.

Arrays in C++

Creating Arrays Using Functions in C++

In C++, arrays are fundamental data structures that allow you to store and manipulate multiple elements of the same type efficiently. Creating arrays using functions can make your code more organized and modular. In this article, we will explore various methods to create arrays using user-defined functions and dynamic memory allocation.

Benefits of Using Functions for Array Creation

When dealing with large and complex codebases, functions offer several advantages in creating arrays:

  1. Reusability: Functions can be called from different parts of the program, allowing you to create arrays in multiple places without duplicating code.
  2. Modularity: Functions help break down the code into smaller, manageable units, making it easier to understand and maintain.
  3. Abstraction: Functions abstract away the implementation details of array creation, making the main code more concise and readable.

Array Declaration Inside A Function

#include <iostream>
using namespace std;
void createArray(int arr[], int size) {
    // Array declaration and initialization
    for (int i = 0; i < size; i++) {
        arr[i] = 0; // Initializing Elements
    }
}
int main()
{
   int a[10]; //Declaring an Array
   int size = sizeof(a)/sizeof(int);
   createArray(a,size)
   return 0;
}

Print the Elements of Array Using Function

Run

#include <iostream>
using namespace std;
void createArray(int arr[], int size) {
    // Array declaration and initialization
    for (int i = 0; i < size; i++) {
        arr[i] = i; // Initializing Elements
    }
}
void printArray(int arr[], int size){
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
}
void updateArray(int arr[],int index,int val){

} int main() { int a[10]; //Declaring an Array int size = sizeof(a)/sizeof(int); createArray(a,size) // Declaring and intializing the array
printArray(a,size); // Printing the elements return 0; }

Output

0 1 2 3 4 5 6 7 8 9

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription