0
// This program demonstrates an array being passed to a function 

#include <iostream>
using namespace std;

void showValues(int [], int) ; //Function prototype.

int main()
{
    const int ARRAY_SIZE = 8;
    int number [ARRAY_SIZE] = {5, 10, 15, 20, 25, 30, 35, 40};

    showValues (number, ARRAY_SIZE);
    return 0;
}   
    //Definition of function showValue.
    //This function accpets an array to integers and 
    //the array's size as its arguments. The contents. 
    //of the array are displayed. 

    void showValues (int nums[], int size)
    {
        for (int index = 0; index < size; index++)
            cout << nums [index] << " ";
            cout << endl;

    }

Using C++, Studying arrays, the program works fine, however; I am not understanding the for loop on the bottom where it says "index < size" Where is "size" getting its value from that the for loop knows when to stop looping?

2
  • It is recommended to use all uppercase symbols for preprocessor to avoid name collision. Previously constants were defined by preprocessor so they were uppercase. When you do not use #define to define a constant, why you still use uppercase and increase probablity of the problem rather than solving it? Why to follow habit blindly? Commented Jul 29, 2015 at 13:16
  • This is one of the examples I used from a book I am reading. I copied the coding exactly as is. I'll keep that tip in mind as I progress. Thank you. Commented Jul 29, 2015 at 14:58

3 Answers 3

3

size is passed as a parameter to the function showValues.

That function is called from main(), passing ARRAY_SIZE as that parameter.

ARRAY_SIZE is defined as the size of the array.

Actually, you could write

int number [] = {5, 10, 15, 20, 25, 30, 35, 40};

since the compiler can figure out the array size from the initialisation array. The function call can be refactored to

showValues(number, sizeof(number) / sizeof(int));

sizeof(number) / sizeof(int) is an idiomatic way of evaluating the number of elements in an array. Some folk prefer sizeof(number) / sizeof(number[0]) since then the code is not sensitive to the type of the array. You can then remove ARRAY_SIZE entirely.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, awesome. I get it now, had to read it a few times but well explained. THANK YOU very much for your help.
1

The showValues (number, ARRAY_SIZE); call

passes the array as well as its size to void showValues (int nums[], int size)

Therefore size in the for loop gets the value 8

Comments

0
 void showValues (int nums[], int size)

The second parameter to the function showValues becomes the size in your for loop.

const int ARRAY_SIZE = 8;

showValues (number, ARRAY_SIZE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.