|
-
Nov 12th, 2007, 04:35 AM
#1
Thread Starter
PowerPoster
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.
Last edited by eranga262154; Nov 12th, 2007 at 04:38 AM.
Reason: Missing code tags
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Nov 12th, 2007, 07:25 AM
#2
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();
}
Last edited by wossname; Nov 12th, 2007 at 07:28 AM.
I don't live here any more.
-
Nov 13th, 2007, 06:36 AM
#3
Thread Starter
PowerPoster
Re: Use of calculated values from different functions inside another function.
 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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Nov 13th, 2007, 07:28 AM
#4
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.
I don't live here any more.
-
Nov 13th, 2007, 11:19 PM
#5
Thread Starter
PowerPoster
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
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Nov 14th, 2007, 02:25 PM
#6
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);
}
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Nov 14th, 2007, 10:17 PM
#7
Thread Starter
PowerPoster
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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Nov 16th, 2007, 03:04 PM
#8
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 
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.
I don't live here any more.
-
Nov 18th, 2007, 10:49 PM
#9
Thread Starter
PowerPoster
Re: Use of calculated values from different functions inside another function.
 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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|