Change Function Signature

I am new to C ++, and now there is one thing I want to clarify. When I look through the tutorial, there is this program that stores user input in an array and gives the sum of all numbers when the user exits the program:

//PROTOTYPE DECLARATION:
int readArray(int integerArray[], int maxNumElements);
int sumArray(int integerArray[], int numElements);
void displayArray(int integerArray[], int numElements);

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "This program sums values entered\n";
    cout << "Terminate the loop by entering a negative number" << endl;     
    //store the numbers from the user into a local array 
    int inputValues [128];
    int numberOfValues = readArray(inputValues, 128);
    //output the values and the sum of the values
    displayArray(inputValues, numberOfValues);
    cout << "The sum is " << sumArray(inputValues, numberOfValues) << endl;
    return 0;
}    
int readArray(int integerArray[], int maxNumElements)
{
    int numberOfValues;
    for(numberOfValues = 0; numberOfValues < maxNumElements; numberOfValues++)
    {
        //fetch another number
        int integerValue;
        cout << "Enter next number: ";
        cin >> integerValue;
        if (integerValue < 0)
        {
           break; 
        }
        //otherwise store the number into the storage array
        integerArray[numberOfValues] = integerValue; 
    }
    //return the number of elements read
    return numberOfValues;
}
//displayArray - display the members of an array:
void displayArray(int integerArray[], int numElements)
 { 
    cout << "The value of the array is:" << endl;
    for(int i = 0; i < numElements; i++)
    {
        cout << i << ":" << integerArray[i] <<   endl;
    }
    cout << endl;
}
//sumArray 
int sumArray(int integerArray[], int numElements)
{
    int accumulator = 0;
    for(int i = 0; i < numElements; i++)
    {
        accumulator += integerArray[i];
    }
    return accumulator;
}

My questions:

  • Is it necessary to declare local variables in each function (e.g. int inputValues [128];)?
  • Would it be correct to store the input in the arguments that were declared in the function prototype? For example, is it possible to just save everything in integerArray[]instead of creating a storage array integerValue?

This may seem obvious, but I want to understand this in order to avoid mistakes in the future.

+4
3
  • inputValues ​​, .

    int inputValues [128];
    int numberOfValues = readArray(inputValues, 128); //passing array to function
    

. .

, inputValues ​​ . .

int readArray(int (&integerArray)[128]);

, , , , , .

Edit:

@Kevin, , .

template<size_t N> int readArray(int (&integerArray)[N]);
+2

, , .. , , , /.

int inputValues[128]; //memory allocation

.

int numberOfValues = readArray(inputValues, 128);    

. , .

+2

.

  • :

"array of T" " T", " T"

  1. :

.

These two together, I hope, will help you see that when you declare a function, as int readArray(int integerArray[], int maxNumElements), integerArrayin fact, is simply a pointer to the first element of the first argument. You call readArray(inputValues, 128), therefore the parameter is integerArrayequivalent &intputArray[0].

+2
source

All Articles