Can I make a function return an array?

Usul

Golden Member
Nov 3, 2000
1,016
0
0
Can I make a function return an array?
Sorry for the dum question.


I'm a newbye in programmation, as you can see....
 

Usul

Golden Member
Nov 3, 2000
1,016
0
0
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....
 

KarlHungus

Senior member
Nov 16, 1999
638
0
0
If you are just using the function to initialize the array, have you considered just making a simple array class?
 

RedRooster

Diamond Member
Sep 14, 2000
6,596
0
76
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.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
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.
 

BFG10K

Lifer
Aug 14, 2000
22,709
2,979
126
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?
 

Usul

Golden Member
Nov 3, 2000
1,016
0
0
Wanderful, how?

*just realize i wasted my time in that class last semester*
 

randomlinh

Lifer
Oct 9, 1999
20,853
2
0
linh.wordpress.com
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
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
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;
}
 

Usul

Golden Member
Nov 3, 2000
1,016
0
0
Ok, thanks.
Now works

But seriosly, what is a good book ? Just over &quot;for dummies &quot; lever, but kind of complete..?
 

BFG10K

Lifer
Aug 14, 2000
22,709
2,979
126
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.
 

Usul

Golden Member
Nov 3, 2000
1,016
0
0
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!
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |