How to multiply without the * sign using recursion?
<p>so as homework for a programming class on python we're supposed to multiply to integers (n,m) with each other WITHOUT using the * sign (or another multiplication form). We're supposed to use recursion to solve this problem, so i tried just adding n with itself, m number of times. I think my problem is with using recursion itself. I have searched on the internet for recursion usage, no results. Here is my code. Could someone point me in the right direction?</p>

<pre><code>    def mult(n,m):
        """ mult outputs the product of two integers n and m
            input: any numbers
        """
        if m &gt; 0:
            return n + n 
            return m - 1
        else:
            return 1
</code></pre>

<p>You have the right mechanics, but you haven't internalized the basics you found in your searches.  A recursive function usually breaks down to two cases:</p>

<ol>
<li>Base Case --
How do you know when you're done?  What do you want to do at that point?</li>
</ol>

<p>Here, you've figured out that your base case is when the multiplier is 0.  What do you want to return at this point?  Remember, you're doing this as an additive process: I believe you want the additive identity element <strong>0</strong>, not the multiplicative <strong>1</strong>.</p>

<ol start="2">
<li>Recursion Case --
Do something trivial to simplify the problem, then recur with this simplified version.</li>
</ol>

<p>Here, you've figured out that you want to enhance the running sum and reduce the multiplier by 1.  However, you haven't called your function again.  You haven't properly enhanced any sort of accumulative sum; you've doubled the multiplicand.  Also, you're getting confused about recursion: <strong>return</strong> is to go back to whatever called this function.  For recursion, you'll want something like</p>

<pre><code>mult(n, m-1)
</code></pre>

<p>Now remember that this is a function: it returns a value.  Now, what do you need to do with this value?  For instance, if you're trying to compute 4*3, the statement above will give you the value of 4*2,  What do you do with that, so that you can return the correct value of 4*3 to whatever called this instance?  You'll want something like</p>

<pre><code>result = mult(n, m-1)
return [...] result
</code></pre>

<p>... where you have to fill in that <strong>[...]</strong> spot.  If you want, you can combine these into a single line of code; I'm just trying to make it easier for you.</p>

python recursion