I'm just started to learn programming so please bear with me if it's a stupid question:
Why do we have to pass a dynamically allocated array from main to function? For example, in python I think there is no need right? So why do we do it in c++? why can't the function directly access the array?
In the code below, if I don't pass arr_ptr[] as an argument to the function, it doesn't work. I was wondering why that is the case. Since the array is dynamically allocated, why can't the function directly access the array through the pointer??
#include<iostream>
using namespace std;
//function sums the values in array
double sumArray(double arr_ptr[],int size)
{   double sum=0.0;
    for (int i=0;i<size;i++)
    {
        sum=sum+arr_ptr[i];
    }
    return sum;
}
int main()
{
    int num_of_vals=0;
    cout<<"Enter number of values to be summed: ";
    cin>>num_of_vals;
    double* arr_ptr=NULL; //creates double ptr
    arr_ptr= new double[num_of_vals]; //dyn array
    for (int i=0;i<num_of_vals;i++)
        {   cout<<"enter value #"<<i<<" out of " <<num_of_vals-1<<" to be 
             summed:";
            cin>>arr_ptr[i];
        }
    int sum=sumArray(arr_ptr,num_of_vals);
    cout<<"sum of values is:"<<sum<<endl;
    delete[] arr_ptr;
    return 0;
}
 
     
    