March 10th, 2003, 04:17 AM
I want to send an array to a function, do something with it inside, and then return it. Then later send it to another function and return it and so on.
How do I do this? Itīs easy with a regular int or something, but I canīt get it to work with arrays. The array looks like this int f.
Can you please tell me how to declare the array, and how to send/return it to/from functions? And how to declare the functions (if itīs any different from regular int functions)
I guess I donīt have to tell you that Iīm pretty new to this :)
March 10th, 2003, 09:37 AM
i don't know c++ but i think it'll be the same situation in c, which is like this:
you never pass a whole array to a function as such - it's impossible. what you actually do is pass a pointer to the array. even if you see code that appears to be passing an array to a function, it's in fact passing a pointer. what this means is that when you alter the array (via a pointer to it), the array gets effected directly, externally to that function - so *no need for any return in the function*, which is pretty handy. even if you don't understand pointers it doesn't matter too much in this particular case as you can make use of them fairly easily almost without realising you're using them.
so bearing in mind your array is f you'd define the function like:
and you call that function like:
void function(int f) /* a pointer to f is being passed rather than the array itself */
f=12; /* treat it as usual array (although in fact you're using pointers) */
/* and no need for return - the array has been changed directly */
so the array name needs passing (which actually behaves as a pointer to the array) to the function.
it maybe a little different in c++. not sure.
Last edited by balance; March 10th, 2003 at 09:40 AM.
March 10th, 2003, 09:42 AM
Your code worked perfectly, so I guess that it isnīt different from C in this particular thing.
March 10th, 2003, 12:36 PM
I am fairly certain that this is the same way you would do it in C++. It adds nothing or take nothing away from the way you would do it in C.
There is another method of doing this, though, which is legal in both C and C++:
Since you are essentially passing a pointer into the function, you can actually accept it as a pointer, and it will still work. Ex: Instead of doing this:
void function(int f)
you can do this:
void function(int *f)
and the following code will still work:
In fact, the above array accessing code can be changed to pointer notation, as well. So, instead of doing this:
you can do this:
AND, this will work with either of the above two function headers. So, arrays and pointers are really interchangable.
March 12th, 2003, 10:01 PM
How can you make a void function when you are setting an array in the parameters? What is the point of a parameter if nothing is returned to it? I don't understand.
March 13th, 2003, 06:21 AM
Question 1: How can you make a void function when you are setting an array in the parameters?
place void as the return type of the function to make it void. The fact that you are accessing an array that is passed in as one of the parameters (which you may or may not change) has absolutely nothing to do with the return value of the function. Of course, you can 'return' more than just the return value of the function if you pass in parameters that are pointers to global variables (or references to global vars), as you can change these values (by dereferencing the pointer, or simply storing a value in the reference), which change their values outside of the function, as well (i.e. any changes made are made directly to the global vars, not the copies inside the function). Passing in an array is the same thing as passing in a pointer to an array, and therefore this global array's elements can be changed. Therefore, in a sense you are 'returning' more than just the return value of the function.
Question 2: What is the point of a parameter if nothing is returned to it?
Parameters are not only meant for the sole purpose of returning values to the calling mechanism. Take, for instance, a function called sqr, whose purpose is to return the square of the value passed in. You would declare it as so:
double sqr(double x)
This does not pass back any value through x (in fact, if x was changed within the function, it would not change the parameter you passed in - only the copy of that parameter is changed, and its value is forever lost after the function terminates). To return a value, we use the function's return value. It would not make any sense to have this function change the value of x (as a reference or a pointer to some global variable), since you couldn't use the function like this:
printf("2*2 = ",sqr(2));
and, if you couldn't do this, either:
printf("x*x + y*y = ",sqr(x)+sqr(y));
if you didn't want the values of x or y to change after this print statement is run.
I hope this clears things up.
Last edited by Jason Doucette; March 13th, 2003 at 06:23 AM.
March 15th, 2003, 07:46 AM
Pointers can be a confusing topic, so allow me to add my two cents...
Normally when you pass a variable into a function, you are creating a copy of that variable for the function to work on. If you change the value of the variable in the function, it does not affect the original value.
When you use pointers, instead of making a copy of the variable, you are actually telling the function where the original variable resides in memory space and allowing the function to act directly on the original. Therefore, if you make any changes within the function, the orginal variable will be changed as well. That's why you don't need a return value if you are passing pointers as parameters.