General array and function question

There's this example in my book that has two things that I don't understand about it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
void showvalues(int [], int);

int main()
{
	const int ARRAY_SIZE = 10;
	int numbers[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

	showvalues(numbers, ARRAY_SIZE); // are there two variables instead of just one?
	return 0;
}
void showvalues(int nums[], int size)
{     // what's the point of the loop if the entire array is being passed
// doesn't it only need to be passed once?
	for (int index = 0; index < size; index++)
		cout << nums[index] << " ";
	cout << endl;
}

the purpose of the example is to show how to pass an array by reference versus value and the entire array versus the array being passed to the function element by element. I explained the two things I don't understand with comments. I wrote them as if the phrase "to give the output that this code gives" was implied.
Last edited on
closed account (zb0S216C)
science man wrote:
showvalues(numbers, ARRAY_SIZE); // are there two variables instead of just one?

Well, ::showvalues() requires 2 arguments when it's called, so, 2. Is that what you meant?

science man wrote:
1
2
// what's the point of the loop if the entire array is being passed
// doesn't it only need to be passed once? 

You're not passing the array multiple times; such an action occurs once at the point of invocation. The loop is used to transverse through nums (iteration). The loops seems to print each number of the array.

science man wrote:
the purpose of the example is to show how to pass an array by reference versus value and the entire array versus the array being passed to the function element by element.

As far as I can see, no array is being passed by reference. Though, I was thinking along the lines of: int(&Array)[ARRAY_SIZE]; a reference to an array of 10 ints.

Wazzak
Last edited on

Well, ::showvalues() requires 2 arguments when it's called, so, 2. Is that what you meant?

Yes it is. I forgot to say why at the begining of that comment. So why does it require there to be at least two arguments? Also, can you refresh me on what it means in programming terms to have an argument?


You're not passing the array multiple times; such an action occurs once at the point of invocation. The loop is used to transverse through nums (iteration). The loops seems to print each number of the array.

hmm I guess I don't clearly understand, what's the difference betweeen passing by value v.s. reference?
closed account (zb0S216C)
Apologies for the delayed response.

science man wrote:
So why does it require there to be at least two arguments?

It requires 2 arguments because the prototype says so. See:

void showvalues(int [], int);

This is your prototype. The parameter list (between the parentheses) clearly indicates that this function requires 2 arguments to be passed to it when called; an int*/int and an int.

science man wrote:
Also, can you refresh me on what it means in programming terms to have an argument?

An argument, not a quarrel, is a piece of data you give to a function when you call it. For instance:

1
2
3
4
5
6
void Function(int X);

int main()
{
    Function(10);
}

In the above code, when ::Function() is called, 10 was passed to it. In this context, 10 is an argument.

science man wrote:
what's the difference betweeen passing by value v.s. reference?


Passing-By-Value:
Passing-by-value generates a copy of the argument. Any changes made to the argument will not affect the original argument. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
void Function(int X);

int main()
{
    int A(10);
    Function(A);
}

void Function(int X)
{
    X = 0;
}

In this example, A is passed to ::Function() by value. The value of A is copied into the X parameter of ::Function(). X is not bound to A in anyway. Therefore, modifications to X do not affect A.

Passing-By-Reference
Passing by reference means you'll be working with the original argument. A reference parameter is bound to the argument that is passed to it. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
void Function(int &X);

int main()
{
    int A(10);
    Function(A);
}

void Funtion(int &X)
{
    X = 0;
}

In this example, when A is passed to ::Function(), X, ::Function()'s parameter, is bound to A. As a result, modifications to X affect A, because X is referring to A (X is bound to A). Note that the object a references refers to is called a referent.

Wazzak
Last edited on
I'm sorry I looked through my book for the concept and discovered that the pass by reference section was a section I skipped. (do to time I had to the homework I had in the past) Anyway back my original question(s), I know the prototype says it requires but that's exactly what I'm asking about meaning why does there need to be two variables to produce the output it produces (which is the contents of the array)?



The loop is used to transverse through nums (iteration). The loops seems to print each number of the array.

Why does it need to? Why can't the contents of the array the displayed in one simple shot like this? cout << numbers[ARRAY_SIZE]? Also, how does the function "showvalues" know that size is ARRAY_SIZE?
The default method of passing arguments is by value. In order to pass arguments by reference, one must explicitly specify in the function prototype that this is what one is doing.

Arrays, however, are different than other variables in C++. They're not passed by value or reference. Instead, they're passed by address in the form of a pointer to the first element.

No size information is passed to the function, unless it is manually supplied via another argument to the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

void func(int a[]) 
{
	cout << "Size of a:         " << sizeof(a) << '\n' ;

	// Because a is just a pointer, you can do things like:
	++a ;  // which you couldn't if it was an actual array type.
	       // and it will have no effect on arr in main.
}

int main()
{
	const int arrSize = 100 ;
	int arr[arrSize] ;

	cout << "Size of arr:     " << sizeof(arr) ;
	cout << "\nSize of arr[0]:    "<< sizeof(arr[0]) ;
	cout << "\nElements in arr: " << sizeof(arr) / sizeof(arr[0]) << '\n' ;

	func(arr) ;

	cout << "Size of int*:      " << sizeof(int*) << '\n' ;
}



If you compile and run that, you'll know why the second parameter is needed. You lose information about the array when passing this way.

Topic archived. No new replies allowed.