how to call a function with an array in the signature

My question is how do I call this function in my main()? I don't need to pass the function any information at first, I just need the board to be printed as is, then I can modify it later on with user inputs. I have COLS defined as a global constant, but I don't have char grid initialized anywhere, is that a problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 void initGrid(char grid[] [COLS]) // creates and initializes the board
{
	const int OUTLINEROW = 8;
	const int OUTLINECOL = 8;

		  char border[OUTLINEROW][OUTLINECOL] = {  {' ', ' ', '0', '1', '2', '3', '4', ' '},
												{' ', '+', '-', '-', '-', '-', '-', '+'},
												{'0', '|', '*', '*', '*', '*', '*', '|'},
												{'1', '|', '*', '*', '*', '*', '*', '|'},
												{'2', '|', '*', '*', '*', '*', '*', '|'},
												{'3', '|', '*', '*', '*', '*', '*', '|'},
												{'4', '|', '*', '*', '*', '*', '*', '|'},
												{' ', '+', '-', '-', '-', '-', '-', '+'} };
		for(int row = 0; row < OUTLINEROW; row++)
		{
			for(int col = 0; col < OUTLINECOL; col++)
			{
				cout << border[row][col] << " ";
			}

			cout << endl;
		}
}
I am in my first C++ class myself, but feel I have a decent handle on functions. I believe that in order to use an array in a function, the array needs to be declared but not necessarily initialized. Furthermore, I don't see the character array grid[] used anywhere in your function, so it doesn't need to be listed as a parameter for your function. I also don't see COLS used anywhere in your function so that doesn't need to be a parameter either. To write a function with no parameters necessary (i.e. don't need to pass the function any information) then write it as:

1
2
3
4
5
6
void initGrid();
{
......

return;
}


See http://www.cplusplus.com/doc/tutorial/functions/ for more info.

Since this function is not returning a value either (which is specified by "void") don't forget to put just plain "return;" at the end of your function. I hope this helped!
unfortunately I must use:

 
void initGrid(char grid[] [COLS])


as my function paramaters because this is how it is listed in the instructions so I guess I have to manipulate my code to fit this function.
I guess my question is what kind of variable is COLS ? What role does it play in your program?
COLS = 8 since my array will have 8 columns
I had typed in:
 
initGrid(grid, COLS);


and it worked the first time, but now I must have changed something else because it's not working anymore :(
To use COLS in your function, you need to do one of two things. Either:

(1) Label a parameter of your function as an integer. For example:
1
2
3
4
void initGrid(char grid[], int columns)
{
Function here.
}

If you choose to do this, you need to input an integer every time you call your function. For example:

1
2
3
4
main()
{
initGrid(charArray, COLS);

You can input any integer variable when you call the function initGrid. Notice you also need to input some character array (in this example, charArray) when you call your function. This is the array that will be modified in your function initGrid, should you choose to.

This option would be best if you are going to be using different numbers of columns when calling your function. It gives you the flexibility to input different integers each time you call the function.

(2) Use your global variable COLS inside of your function. Any time you need to use its value (8) you can use it. Then you would not need to declare it as a parameter in your function, because it would always be used the same. For example:

1
2
3
4
void initGrid(char grid())
{
Function here.
}


Then when calling your function, you would input:

initGrid(charArray);

This option would be ideal if your function will always use 8 for the number of columns. Everytime you want to use the value of COLS within your function, just type COLS.

Do only one of the two above options, not both (or else it still won't work).

You are using the character array border[][], declared within your function, to display from. This function is created every time your function is called, its values are displayed (via cout << ) and when the function gets through, the array disappears. That means that every time you call this function, the array border[][] will display the same thing. If that is okay, then skip the rest of this.

However if you are trying to make a function that will adjust an array that is declared in the main function and exists even while the initGrid function is not running, you need to change your code. Like I stated earlier, you use an array as one of the input parameters and to me that seems you are trying to adjust an array that exists outside of the initGrid function. In order to adjust this array, you need to use its name that is identified in the parameters of the initGrid function during the adjustments in the initGrid function. For example use:

 
grid[OUTLINEROW][OUTLINECOLUMN]= { {' ', ' ', '0', '1', '2', .....}} 


No use to make up an array inside of this function if you want an array that exists outside of the function. Replace all uses of border[][] with grid[][]. This way, when you call your initGrid function, you input an existing array as the parameter. Then when your intiGrid function is finished running, the board is still stored in whatever array you used as the input. For example:

1
2
3
4
5
6
7
main()
{
int size=8;
char charArr[size][size];
initGrid(charArr, size);
}
(

Now the grid you created in the initGrid function is stored in the array charArr. This array charArr can be adjusted later as needed.

I would highly recommend reviewing the function pages on this website:

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

I hope this has helped!
Topic archived. No new replies allowed.