31) Passing Arrays to Functions¶
This is also from the class where I didn’t have my register with me. I’ll try not to miss anything, but some obscure thing might end up being asked on in my exams to come.
We can both pass and return arrays from functions. The syntax for passing an array to a function is:
1 2 3 4 5 6 7 8 9 | |
I’ll get to why the syntax is valid in Line 7. All you need to know is, Line 1 is how you pass a 1D array to a function. Then you can use it the same way you would normally use an array.
For passing a 2D array, the syntax is the same, but you type the row and column size.
1 2 3 4 5 6 7 8 | |
And again you might notice on line 5 that there’s a value missing. But this is still valid. The rule is, the square brackets closest to the name of the array can be left empty. It’s completely optional to do so. So for a 1D Array, the square brackets can be left empty. For a 2D Array, the Column Size is necessary. For a 3D Array, which would be [x][y][z], it can be passed as [][y][z] as well.
You don’t have to do 3D Arrays. They’re distracting. The bigger thing to focus on now, is Pointers.
That’s also how Arrays are returned. With Pointers. But first you need to actually figure out what Pointers are. Make sure to revise on 30) Passing Variables by Reference, specifically in the section where the computer’s memory and storage are explained. Pointers work on that concept.