Use of calculated values from different functions inside another function.
Hi all,
I want to use some calculated values from different functions inside another functions. I'll explain it inthis way,
Say I have following functions.
Code:
void functionOne(int x, int y)
{
// Do some work and find some values,
// say int a, int b
// Need to send those values to mainCal()
}
void functionTwo()
{
// Do some work and find some values,
// say double dd,
// Need to send those values to mainCal()
}
Now I want to use the above calculated values inside the following function.
Code:
void mainCal()
{
// Do some process with a, b, dd
}
I think it is clear for you. How can I do that.
Re: Use of calculated values from different functions inside another function.
To the first function, pass in a pair of int pointers which you can store the values in. To the second, use a return type of double.
Code:
void functionOne(int x, int y, int* a, int* b)
{
// Do some work and find some values,
// say int a, int b
// Need to send those values to mainCal()
*a = 123;
*b = 456;
}
double functionTwo()
{
// Do some work and find some values,
// say double dd,
// Need to send those values to mainCal()
return dd;
}
Code:
void mainCal()
{
int a = 0;
int b = 0;
double dd = 0.0;
// Do some process with a, b, dd
functionOne(100, 200, &a, &b);
dd = functionTwo();
}
Re: Use of calculated values from different functions inside another function.
Quote:
Originally Posted by wossname
To the first function, pass in a pair of int pointers which you can store the values in. To the second, use a return type of double.
Thanks, I got the point. But I have to one thing to clarify. One of my function calculated seven values, and that is the maximum. So it wont a mess in this way.
Re: Use of calculated values from different functions inside another function.
It would be better to do this...
Code:
#include <stdio.h>
typedef struct __tCalculations
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
}tCalculations;
void Calculate(tCalculations* values)
{
values->a = 1;
values->b = 10;
values->c = 100;
values->d = 1000;
values->e = 10000;
values->f = 100000;
values->g = 1000000;
}
int main()
{
tCalculations calcs;
Calculate(&calcs);
printf("calcs.a = %d\n", calcs.a);
printf("calcs.b = %d\n", calcs.b);
printf("calcs.c = %d\n", calcs.c);
printf("calcs.d = %d\n", calcs.d);
printf("calcs.e = %d\n", calcs.e);
printf("calcs.f = %d\n", calcs.f);
printf("calcs.g = %d\n", calcs.g);
return 0;
}
Bundling them all up into a struct keeps them together andmeans you only have to pass a single item around.
Re: Use of calculated values from different functions inside another function.
Thanks pal. It make me sense. If I got any issues, I'll be back here :)
Re: Use of calculated values from different functions inside another function.
Woss pretty much nailed it.
The only thing I would add is that if you're using C++, you can use references instead of pointers (and the typedef for the struct is not necessary).
Code:
void dosomething(mystruct& s)
{
s.a = 3;
}
int main()
{
mystruct foo;
dosomething(foo);
}
Re: Use of calculated values from different functions inside another function.
Actually I don't like pointers. Most of the case I use struct as well, it is so easy to me use.
Re: Use of calculated values from different functions inside another function.
Good point about the typedef though sunburnt. Actually I put that in absentmindedly out of habit. Its part of the coding standard we use at work its second nature to me now. Actually its really handy when grepping through 15MB of source for a struct definition in a hurry :D
I tend to write answers in C if the language is not specified then its guaranteed to be legal in both languages.
Eranga, you shouldn't really avoid pointers, its best to get to know them well early on, then you'll be able to read the code written by others more easily.
Re: Use of calculated values from different functions inside another function.
Quote:
Originally Posted by wossname
Eranga, you shouldn't really avoid pointers, its best to get to know them well early on, then you'll be able to read the code written by others more easily.
Thanks. Actually why I'm avoid those stuff is, still I'm new for C++ and still don't have a good idea how to control those stuff.;)