how do i pass agruments to a function,
perform math ops on the values,
then return the values ( more than one value )
thanx in advance
how do i pass agruments to a function,
perform math ops on the values,
then return the values ( more than one value )
thanx in advance
someone once told me i could make money at this ...
I'm going to sleep and don't have much time, but you can't normally return more than 1 value, unless you do it in a weird way. Use pointers for more than 1 value. Don't you have a book on C?
You pass the memory address of the argument to the function by putting the & sign (reference) in front of the variable name.
In the function use the * (dereferencing) to access the value of result:
Code:void add(int a, int b, int *result) { *result = a + b; } int main() { int a = 10; int b = 2; int result; /* pass address of result */ add(a, b, &result); printf("Result = %d\n", result); return 0; }
how bout sumthin like this:
int buffer[3];
main(){
add();
printf("%i, %i, %i:, buffer[1], buffer[2], buffer[3]);
}
int add(void){
buffer[1] = 1+1;
buffer[2] = 2+2;
buffer[3] = 3+3;
return 0;
}
someone once told me i could make money at this ...
>then return the values ( more than one value )
Why not just write the function in such a way that it takes the values and returns the one result of the operation on those variables, then you can handle the management of variables in main.
This saves you from having to use global variables and you keep the parameter list for the add function short and easy to read.Code:#include <stdio.h> #define BUFFER_SIZE 5 static int addTwo ( int one, int two ) { return one + two; } int main ( void ) { int numberList[BUFFER_SIZE], x; /* For adding two numbers together, just call AddTwo. */ for ( x = 0; x < BUFFER_SIZE; x++ ) numberList[x] = addTwo ( x + 1, x + 1 ); /* For more than two, add the result of addTwo to a ** number, or call addTwo more than once as long as ** the order in which addTwo is called doesn't matter. */ numberList[3] = 10 + addTwo ( 4, 5 ); numberList[4] = addTwo ( 6, 6 ) + addTwo ( 1, 2 ); return 0; }
-Prelude
My best code is written with the delete key.
While that solution would work (barring the missing closed quotation on the printf, seems you hit colon instead), it doesn't actually involve any variable passing. Instead you are declaring a global variable that all functions have unrestricted access to.
To tell if a function is passed something, look at the part between the parenthesis after the function name. Anything in there is passed to the function. For instance:
has one variable passed to it. It is an integer with the identifier "bar".Code:int foo(int bar)
In C, and all the languages I've ever used, variables are normally passed by value. This means when you call a function, the program makes a copy of everything you are passing to the function (an integer, in our example) and give the function that. Because the function is working with copies instead of the original, any changes made inside the function will not carry back to the calling function once the routine is complete.
The solution to this? Passing by reference. It is a rather complicated topic, so I can't go over it all now, but imagine it as passing the address of the data to a function instead of a copy of the data. This way, the program can go directly to this address and modify the data there, so any changes made are still relevant once the the called function is done.
There are dozens of posts on the syntax involved, so if you're interested, search the boards for "pointer". If you decide it isn't quite what you need, the other posts have good recommendations.
This is a stupid solution to the problem at hand but it does accomplish exacly what you are wanting:
As you can see, each index in the array represents a different variable. I call this way stupid because you are going to have to invest a little more time into error handling, also it makes your code less sensible to a more experienced programmer.Code://this function will subtract 5 from x, add 1 to y and divide z by 6 long *dosomething(long x, long y, long z) { long *rtrn_vals = (long *)malloc(12); if(rtrn_vals != NULL) { rtrn_vals[0] = x-5; rtrn_vals[1] = y+1; rtrn_vals[2] = z/6; } return rtrn_vals; }