# passing data between functions

• 12-08-2006
nesagsar
passing data between functions
I am writing a program that is supposed to reduce a fraction to its simplist terms. I can only use <iostream.h> and <string.h>. I have a function working that finds the greatest common divisor and I have a function that will take that and simplify the fraction.

How do I get the data from the first function (the GCD) into the second function (so It can do the math)?
• 12-08-2006
robatino
The GCD function should have its output as its return value. Also, you should use <iostream> and <string> (I don't use strings myself so I'm not sure of the name but the .h headers are not standard in C++). Also, for efficiency you should use the Euclidean algorithm to compute the GCD if you weren't doing that (trying to find common factors of the numerator and denominator is horribly inefficient by comparison).
• 12-08-2006
Mario F.
You can use a function call as an argument to another function as long as the previous function return type is the same (or is convertible) as the expected parameter type.

int function1(int);
void function2(int, int);

int value = 7;
function2(function1(value), 13);
• 12-08-2006
nesagsar
How would that fit into this exactly?
Code:

```int main() {         int w, x;   char junk;         cout << "This program calculates the GCD of a fraction and simplifies the fraction\n";         cout << "Value of first numerator: ";         cin >> w;         cout << "Value of first denominator: ";         cin >> x;         cout << "\nThe Greatest Common Divisor of "             << w << " and " << x << " is " << GCD(w, x) << endl;   cout << "\nThe Simplified Fraction is "             << simplify(w,x) << endl;   cin >> junk; //This input allows for display.         return 0; }```
• 12-08-2006
Mario F.
It wouldn't. But then and again you asked how could you pass the result of GCD into another function. You need to create a third argument on the simplify() function that will take the gcd
• 12-08-2006
nesagsar
Like this?
Code:

`int simplify(x, y, GCD);`
• 12-08-2006
Mario F.
C'mon you can do better than that...

Think...

What is the return type of the gcd function?
What does that number represent?
How are you going to use it inside the simplify function?
• 12-08-2006
nesagsar
GCD returns type int.
GCD is the greatest common divisor.
I am going to divide W and X by the GCD to get a new fraction.
• 12-08-2006
Mario F.
So? What is that you need to pass to simplify()?

an int representing the numerator
an int representing the denominator
and...?

Then look at my previous example showing how to use a function call inside another.
• 12-08-2006
nesagsar
This is GCD.
Code:

```int GCD(int a, int b ) {         int remainder, quotient, divisor,numerator;                 if (a > b)                 {        remainder=a%b;                 quotient=a/b; divisor=b;                 }else{                         remainder=b%a;                 quotient=b/a;divisor=a;                 }                 while( remainder!=0 )                 {                 numerator=divisor;                 divisor=quotient;                 remainder=numerator%divisor;                 quotient=numerator/divisor;                 }         return divisor; }```
• 12-08-2006
Mario F.
Good luck solving this problem. The solution I already gave you.
• 12-08-2006
robatino
Your GCD code is way too complicated. Look at the Wikipedia pseudocode. You can do it in around 5 lines and using only remainder and not division.

http://en.wikipedia.org/wiki/Euclidean_algorithm