Passing an array created in function to other functions I'm beginning my programming adventure in C++. I want to write a small program, which has to create an array in function create (size has to be defined in create function) and transfer them to function display which will print entered values.

#include &lt;iostream&gt;

using namespace std;

int *create(int &amp; n);
void display(int * arr, int n);

int main()
{
    int n;
    int * arr = create(n);
    display(arr, n);


    delete[] arr;

    return 0;
}

int *create(int &amp; n) {
    cout &lt;&lt; "How many elements? ";
    cin &gt;&gt; n;
    int * arr = new int[n];
    int element;
    for(int i = 0; i &lt; n; i++) {
        cout &lt;&lt; endl &lt;&lt; "Enter " &lt;&lt; i &lt;&lt; " array element: ";
        cin &gt;&gt; element;
        arr[i] = element;
    }

    return arr;
}

void display(int * arr, int n) {
    for(int i = 0; i &lt; n; i++) {
        cout &lt;&lt; endl &lt;&lt; i &lt;&lt; " element: " &lt;&lt; arr[i];
    }
}


I'm looking for every criticism, optimization suggest and everything which will help me be a better programmer and not to learn bad habits from the beginning.
First, the usual criticisms:


Don't use using namespace std;
Don't use raw new and delete
Include the headers your code uses (e.g. &lt;iostream&gt;)
Prefer prefix ++i over postfix i++ as a general habit
Prefer to omit the implied return 0; from main (unless you're trying to emphasize that the return value is different via different codepaths, which doesn't apply in this case)


Then, the point specific to your API design:


Your function create has two outputs: the pointer to a new array and the size of the new array, n. The former is returned as the return value; the latter is passed in by reference (the out-parameter pattern). Instead of returning these two pieces of information by two different routes, you should unify them: Either pass in two out-parameters (yuck!) or pass out two values by return.


Taking the latter approach (two values by return) and applying all our rules except "no raw new and delete", we end up with this:

#include &lt;iostream&gt;
#include &lt;utility&gt;

std::pair&lt;int *, int&gt; create() {
    int n;
    std::cout &lt;&lt; "How many elements? ";
    std::cin &gt;&gt; n;
    int *arr = new int[n];
    for (int i = 0; i &lt; n; ++i) {
        int element;
        std::cout &lt;&lt; std::endl &lt;&lt; "Enter " &lt;&lt; i &lt;&lt; " array element: ";
        std::cin &gt;&gt; element;
        arr[i] = element;
    }
    return {arr, n};
}

void display(int *arr, int n) {
    for (int i = 0; i &lt; n; ++i) {
        std::cout &lt;&lt; std::endl &lt;&lt; i &lt;&lt; " element: " &lt;&lt; arr[i];
    }
}

int main() {
    auto [arr, n] = create(n);
    display(arr, n);
    delete [] arr;
}


The JavaScript-looking [arr, n] is C++17 syntax, but don't worry, we're about to get rid of it in our next refactor.

Applying "no raw new and delete" allows us to simplify much further:

#include &lt;iostream&gt;
#include &lt;vector&gt;

std::vector&lt;int&gt; create() {
    int n;
    std::cout &lt;&lt; "How many elements? ";
    std::cin &gt;&gt; n;
    std::vector&lt;int&gt; result;
    for (int i = 0; i &lt; n; ++i) {
        int element;
        std::cout &lt;&lt; std::endl &lt;&lt; "Enter " &lt;&lt; i &lt;&lt; " array element: ";
        std::cin &gt;&gt; element;
        result.push_back(element);
    }
    return result;
}

void display(const std::vector&lt;int&gt;&amp; vec) {
    for (int i = 0; i &lt; int(vec.size()); ++i) {
        std::cout &lt;&lt; std::endl &lt;&lt; i &lt;&lt; " element: " &lt;&lt; vec[i];
    }
}

int main() {
    auto vec = create(n);
    display(vec);
}


The last thing I'd consider doing is eliminating mutation from the code. You've got these variables int n and int element that aren't initialized, and then you use std::cin &gt;&gt; ... to mutate their values. It might be more straightforward to write an "int-getting function" and use it consistently. Plus, this gives us a place to hang the missing std::flush:

#include &lt;iostream&gt;
#include &lt;vector&gt;

template&lt;class T&gt;
T input(const char *prompt) {
    T result;
    // Print out everything in `prompt` (C++17 syntax)
    std::cout &lt;&lt; prompt &lt;&lt; " " &lt;&lt; std::flush;
    std::cin &gt;&gt; result;  // Should we add some error-checking?
                 // What if the user types "hello world" here?
    return result;
}

std::vector&lt;int&gt; create() {
    std::vector&lt;int&gt; result;
    int n = input&lt;int&gt;("How many elements?");
    for (int i = 0; i &lt; n; ++i) {
        result.push_back(input&lt;int&gt;("Enter the next array element:"));
    }
    return result;
}

void display(const std::vector&lt;int&gt;&amp; vec) {
    for (int i = 0; i &lt; int(vec.size()); ++i) {
        std::cout &lt;&lt; std::endl &lt;&lt; i &lt;&lt; " element: " &lt;&lt; vec[i];
    }
}

int main() {
    display(create(n));
}


Notice that I changed your prompt from "Enter the 42 array element:" to simply "Enter the next array element:". This was because I didn't want to bother explaining how to write a variadic-template version of input that would accept input&lt;int&gt;("Enter the ", i, " array element:"). It's possible — even trivial, in C++17 where we can write (std::cout &lt;&lt; ... &lt;&lt; prompt_pieces) &lt;&lt; " " &lt;&lt; std::flush — but I want to keep the simple code simple.

Now we've localized all the icky "state mutation" into the innards of input&lt;int&gt;; we don't have to think about mutation while we're reading create.