Hello all,
I have recently gotten back into programming after some years away. I'm currently attending classes for Comp Science, and I've read back up on the basics. I'm currently experiencing a learning block and I haven't been able to determine how this works exactly after searching around and trying to understand this. It would be of great assistance someone can help me better understand this.
I have these 2 functions within my program that work perfectly fine. I was able to get some assistance from my professor to get this to work.
(Rule is - We must write a recursive function, no for loops,while/do loops, etc... We must also not use any arithmetic operators i.e (+, -, %, /)
I was able to determine the first addition function on my own by using increment/decrement operators, however I was unable to figure out the multiplication function without the use of the arithmetic + operator return the function properly. I was able to get assistance and determined the work-around, but I'm still confused on how this actually works, and would like to get around this before falling too behind on other current studies.
The biggest obstacle I'm currently facing is from the multiply function below. I'm not too experienced on calling functions within other function arguments.. But I'm trying to understand the concept of how...
1. return add(a, multiply(a, --b));
From including the cout messages within the multiplication function, I see the multiply(a, b) runs/returns first even though the add(a,b) function is stated first.
I'm assuming this because the complete multiply argument is met first? add's parameters/closing parenthesis ends after --b in the multiply function argument. Is that correct?
I'm also assuming (but would like confirmation) that the "b" int variable from add(a,b) is being pulled from the multiply(a)
It seems to me that for every multiplication function iteration, add is called twice, why is this case?
Value of (mul) A = 2 Value of (mul) B = 3
Value of (mul) A = 2 Value of (mul) B = 2
Value of (mul) A = 2 Value of (mul) B = 1
Value of (add) A = 2 Value of (add) B = 2
Value of (add) A = 1 Value of (add) B = 3
Value of (add) A = 0 Value of (add) B = 4
Value of (add) A = 2 Value of (add) B = 4
Value of (add) A = 1 Value of (add) B = 5
Value of (add) A = 0 Value of (add) B = 6
Multiplication function: 2 * 3 = 6
Excuse the constant cout of values as I'm trying to determine how the multiplication works exactly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
using namespace std;
unsigned add(unsigned int a, unsigned int b);
unsigned multiply(unsigned int a, unsigned int b);
int main()
{
int displayAdd = add(7, 14);
cout << "Addition function: 7 + 14 = " << displayAdd << endl;
cout << endl;
int displayMultiply = multiply(2, 3);
cout << "Multipltication function: 2 * 3 = " << displayMultiply << endl;
cout << endl;
}
// functions below
unsigned add(unsigned int a, unsigned int b) // works fine
{
cout << "Value of (add) A = " << a << " Value of (add) B = " << b << endl;
if (a == 0) return b;
return add(--a, ++b);
}
unsigned multiply(unsigned int a, unsigned int b) // works fine
{
cout << "Value of (mul) A = " << a << " Value of (mul) B = " << b << endl;
if (a == 0 || b == 0) return 0;
if (b == 1) return a;
return add(a, multiply(a, --b));
}
|