Help with Understanding Recursion and Functions within other Functions

Sep 6, 2019 at 8:16pm
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));
}
Sep 6, 2019 at 8:34pm
no, each call to multiply only calls add once.
you are misreading the output.

2,3
returns multiply (2, multiply(2,2))
2,2
returns multiply (2, multiply(2,1))
2,1 triggers b == 1 and returns 2.
this backs out.
2 is added to 2
that one ends and it backs out
add 2
backs out
adds 2
total is 6.

so why all the add spam?
add calls itself. each call to add can call add again...

if you want to see what multiply does, replace add with a pure adder, not your recursive one, use something like
int add2(int a, int b)
{
//optional print
return a+b;
}

and you will see that it only calls add one time per.
its just that the one call to add in multiply is calling itself again...
Last edited on Sep 6, 2019 at 8:36pm
Sep 6, 2019 at 9:11pm
Ahh I see.

I should have disregarded the first initial output of
Value of (mul) A = 2 Value of (mul) B = 3, as that is simply outputting the current variables without any iterations.

So I assume the case of:


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)


multiply(a,b) will run first even though function add(a, ...) is not satisfied yet then? (Even though add was called first, multiply had its argumentation conditions met first so it iterates first then correct?

2 is added to 2

We retrieved the (a is added to b) value of b from multiply(a)? or did we retrieve this from

if (b == 1) return a;

Where as "return a" of value 2, will now be stored and meet the requirements of add(a, [value needed here])

Taking out the recursive add function and using just a regular add function immediately helped me see that the add function was being called only once per multiply function. I suppose I didn't even experiment with that yet due to the restriction of the assignment.
Topic archived. No new replies allowed.