- [rephrased] how to send data back to the calling function?
Basically you have two options
create the object to hold the data in the calling function, pass a pointer and have the called function use that
int main(void) {
int src1[10][10] = {0};
int src2[10][10] = {0};
fx(src1, src2);
}
or use malloc()
(and friends) in the called function then return that to the caller which will be responsible to free
the resources.
typedef int m2[10][10];
int main(void) {
int src[10][10] = {0};
m2 *dst = fx(src);
// use dst
free(dst);
}
m2 *fx(int m[10][10]) {
m2 *x = malloc(10 * 10 * sizeof (int));
// work with m to change x values
return x;
}
A third option, to be frowned upon (no example code), is to use global variables.
- how to pass 5 or 6 variables between functions?
Basically, just specify them in the calling code, like
printf("%s bought %d %s for %.2f\n", name, quantity, "apples", quantity * price);
Or, if the variables are related, you can try grouping them in a struct
and pass a pointer
struct GroupData {
char name[100];
int quantity;
char item[100];
double price;
};
struct GroupData x = {"John", 42, "apple", 0.31};
workwithdata(&x); // can also pass a copy of the struct
// workwithcopy(x);
- when to use void function or int function?
use void
when there is no need to use a value from the function
void beep(void); // sounds the bell
use int
when you want to use a value from the function
int currenttemp(void); // returns the temperature