Trying to understand functions + arrays

Hey guys, I'm currently taking a beginner C++ class and failing to understand how arrays and functions work together, I have a test coming up soon and the teacher gave us some problems to help us practice for the test, the only problem being that I am very confused as to what I'm looking at, if someone could help break down this code and explain it to me I would greatly appreciate it!

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
26
27
28
29
30
31
32
33
34
  #include <iostream>
#include <iomanip>
using namespace std;

void modifyArray (int [], int);
void modifyElement (int);

 int main()
 {
 	const int size = 5;
 	int a[size];
 	
 	for (int i = 0; i < size; i++)
 	     a[i] = i * i;
 	     
 	modifyArray (a, size);
 	cout << "a[1] = " << setw(3) << a[1] << endl;
 	
 	modifyElement (a[1]);
 	cout << "a[1] = " << setw(3) << a[1] << endl;
 	
 	return 0;
 }
 
  void modifyArray(int b[], int s)
  {
  	for (int k = 0; k < s; k++)
  		 b[k] += 2;
  }
  
  void modifyElement(int d)
  {
  	d *= 2;
  }
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
26
27
28
29
30
31
32
33
34
 #include <iostream> //allow access to cout, etc, standard c++ header
#include <iomanip>   //allows access to formatting for cout, usually floating point printing. 
using namespace std;  //a bad idea, see 1000 threads on topic in the forums, this allows you to skip typing std:: on cout etc. 

void modifyArray (int [], int);  // a function header, this tells the compiler it exists before it sees the code for it, so it won't crash parsing main which uses the function before it sees it (it parses the file top to bottom). 
void modifyElement (int);  //as above. 

 int main()  //the main program, starting point. 
 {
 	const int size = 5;  // define size as the value 5. 
 	int a[size];   //create an array, called 'a', with size (5) elements. 
 	
 	for (int i = 0; i < size; i++)  //loop 0,1,2,3,4 (5 times)
 	     a[i] = i * i;   //assigns a the values of the squares of i (0, 1, 4, 9, 16) 
 	     
 	modifyArray (a, size);  //calls your function.  changes a. 
 	cout << "a[1] = " << setw(3) << a[1] << endl;  //writes out the second location in a
 	
 	modifyElement (a[1]);  //calls your function. does nothing. 
 	cout << "a[1] = " << setw(3) << a[1] << endl;  // as above. 
 	
 	return 0;
 }
 
  void modifyArray(int b[], int s) // this function takes an array and an integer.  you called it with a and size. 
  {
  	for (int k = 0; k < s; k++) //for all the values in a (this is 0-size like the loop in main to initialize the array) 
  		 b[k] += 2; // each element of the array has 2 added to it.  so it went from 0,1,4,9,16 to 2,3,6,11,18
  }
  
  void modifyElement(int d)
  {
  	d *= 2;  //d which is a[1] which is 3 is multipled by 2, but this is only done to a local copy, so it does not change anything.  no real effect. 
  }


I see, thank you! What would the two cout's end up printing anyways? Would they both be the same number or?
because the 2nd function does not do anything, they print the same value, which I think is going to be 3 (1*1 = 1, 1+2 = 3). Run it and see :)

by the way this is a bit of a big topic for newcomers but...
if you pass by reference into the second function, it WOULD change the data, and the second print would be 6 (3*2).

arrays are passed by reference automatically, but many other things are not. It is generally good practice to pass by reference when the object is large (later you will make your own variable types that are large called classes (and structs on occasion)). That would look like this:

void modifyElement(int &d) //the & makes it by reference.

the sooner you can understand what I just said, the better things will be for you!
Why does the second function not do anything?
because it did not pass by reference.
what it really does, broken down:

make a copy of the input (a[1]) into a 'local' variable 'd'. d is now 3.
modify d to be 6.
discard this modified local variable when the function ends. No assignment is made to modify the array 'a'.

if you added the & as I said, it would be like this:

RENAME a[1] (the input) to a reference called 'd'. d and a[1] are the SAME THING now.
modify d to 6, but d is a[1], so really, modify a[1] to be 6.
upon exit, a is modified, and a[1] is 6.

Topic archived. No new replies allowed.