You can return x, or you can return y, but not both (and certainly not as a float). Also note that just because the letters are the same does not mean that x and y in input have any relation to x and y in main.
You can return x, or you can return y, but not both (and certainly not as a float). Also note that just because the letters are the same does not mean that x and y in input have any relation to x and y in main.
You can't return multiple values that way from a function. Instead, you need to pass a pointer to the variables:
Code:#include <stdio.h> // Receive pointers to the variables void input(int *x, float *y) { printf("Enter an integer: "); scanf("%d", x); printf("Enter a real number: "); scanf("%f", y); } int main(void) { int x = 0; float y = 0.0f; // Pass the addresses of the variables // to the function. input(&x, &y); // Print the values of the variables printf("Got %d and %f\n", x, y); return 0; }
I've heard of pointers but haven't learned about them yet. If I understand your code though the main difference seems to be the asterisk in front of x and y when you "declare" (is that even what it's called) them in input(). also you set initial values in main() and when you call the function you use & instead of the variable type. am I missing any key data about these and are my assumption correct?
thanks for the help by the way?
edit:
also you just wrote the function. would the prototype look like this
or like thisCode:void input(int *x, int *y);
Code:void input(int x, int y);
Last edited by demuro1; 09-26-2008 at 07:05 PM.
Yes, pointers mean you put a * before the type, between the type or beside the name:
int* x
int * x
int *x
All the same thing.
And since pointers hold the address of something, you do indeed need to pass the variables using &, to pass their address.
Perhaps it would be best to study pointers a little before you start using them.