-3

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;
}

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 2
    You *can* give the function access to the array that is local to the calling function (e.g. `main`). Would you like an example? – Beta May 16 '19 at 23:54
  • Can you provide a coded example of what you mean in each language? Because I really don't think I understand what you are asking. – Galik May 16 '19 at 23:58
  • 1
    If you are asking why scope is important, you will learn all sorts of pain when trying to maintain even a medium sized project with all variables declared global (which is what I believe you are talking about.) – Michael Dorgan May 17 '19 at 00:17
  • And you can create dynamically allocated variables at global time, but it can get complicated. https://stackoverflow.com/questions/33492546/c-static-pointers-static-objects-and-dynamic-memory-allocation – Michael Dorgan May 17 '19 at 00:22
  • Using globals is also bad practice in python! You can do bad things also in C++ if you like :-) BTW: Your code looks totally as C! You have data separated from functions. In C++ you have the ability to use classes and objects which clarify the relation from data to methods. If you start with object oriented programming, you will see that the amount of parameter passing will be reduced automatically. I would advice you to read a beginners book for C++! Also dealing with raw arrays is not C++ style! – Klaus May 19 '19 at 07:13

1 Answers1

1

Short answer: you don't. You can always use globals.

That being said: globals are evil. It is often considered to be a bad practice, and a lazy one often.

The thing that guides usually development on C++ that allows it to be used for huge projects it to narrow visibility and responsability. One major goal is to create class invariants. That's hard to guarantee with Python.

Python and C++ are different tools suited for different jobs.

Anyway, you should pass your variables as arguments in Python too: that allows you to reuse code.

Mirko
  • 1,043
  • 6
  • 12