• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Can I make a function return an array?

Usul

Golden Member
Can I make a function return an array?
Sorry for the dum question.


I'm a newbye in programmation, as you can see....
😱
 
Couse I want to use the function to initialize the array, and haven't been to the pointer part of the Computer Science Class yet 🙂

Yeah, I should get a book....
 
What language are you using??
If you're using C/C++, you'll have to use a pointer to the array, but if you're using Java you can just pass the array directly, I believe just the name and perhaps [][] based on the dimensions.
 
If you are passing an array to a functin it's automatically passed as a pointer. This means that any changes you make to it will be automatically reflected in the source array.
 
Couse I want to use the function to initialize the array, and haven't been to the pointer part of the Computer Science Class yet 🙂

Then why not just make the array global?
 
usually, global == bad for a beginner class.

and do this:

void fnInitArray(int array[], int size)
{
for (int x = 0; x < size; x++)
array[x] = 0;
}

adjust accordingly. as someone said, array is a pointer, so it will be passed by reference, and any changes in the function will change the array.

edit: heh, the i of course put italics on 🙂 doh.. changed to x
 
I would agree that this is a good solution....

void fnInitArray(int array[], int size)
{
for (int i = 0; i < size; i ++)
array = 0;
}

I think you added the i , but it didn't show up. Substituting x.

void fnInitArray(int array[], int size)
{
for (int x = 0; x < size; x++)
array[x] = 0;
}
 
Ok, thanks.
Now works 🙂

But seriosly, what is a good book ? Just over &quot;for dummies &quot; lever, but kind of complete..?
 
usually, global == bad for a beginner class.

Yes but beginners get their programs up and running faster. At least that's what I've found.
Also if you make global variables most compilers will automatically initialise them for you.

#include <iostream.h>

const int SIZE = 10;//or whatever you want
int array [SIZE];

void init_array()
{
for (int x = 0; x < SIZE; x++)
array [x] = 0;
}

void main()
{
init_array();
}

Piece of cake. Not only that but everything can see your array and there's no need to pass it around and/or return it. For simple programs it saves a lot of time to use global variables.
 
yes, but the point is to teach them to program from the start... all i get from professors is &quot;if you use global non-constants, you will fail this course&quot;
 
thanks.

Global variables aren't an issue, they take ~5 sec to go thru all the source code, will never find it 🙂

Thanks to everyone!
 
Back
Top