I'm trying to write a recursive functionthat receives a parameter n and returns the multiple of 5.
Example: if we have 5 as the argument, the print out will be :
5
10
15
20
25

My code:

def MultiplyRecursive(r):  
  if r == 1:
    return 5
  else:
    return MultiplyRecursive(r-1) + 5

So, what's your problem? In any case, you need to guard against values of r < 1 as well, returning 0 in such a case.

The question is unclear. It implies that the function is printing a value but the above function has no print statement. One wonders what is it supposed to print if you pass 3 to the function. Is it supposed to be 3, 6, 9, or is it supposed to be 5, 10, 15? I will assume the latter. So here is the code to do that:

def MultiplyRecursive(r):
   if r<1:
      print("Invalid argument")
      return 0
   elif r==1:
      print(5)
      return 5
   else:
      temp = MultiplyRecursive(r-1) + 5
      print(temp)
      return temp

Just be aware that you need to call MultiplyRecursive(n) from within some other function, such as main() or another "helper" function. If you try to run it directly from IDLE you will also see the final returned value displayed in addition to the explicitly printed lines from the function. That is, you would see the last value appear to be printed twice. Alternatively, you could always choose to call MultiplyRecursive thusly:

dummyVariable = MultiplyRecursive(25)

Two more points--
The function name itself was poorly chosen if multiplying by 5 recursively is what it is supposed to do. MultiplyByFive(n) would be more appropriate. The user of the function doesn't need to know that internally it is a recursive function (even though that may have been the homework requirement). But the "five" is certainly something that needs to be known without looking at the implementation details. If printing was also part of this functions role, then perhaps an even better function name would be CountByFive(n).

If the main function was the one that was supposed to do the printing rather than the recursive function (which makes more sense) then your original function would have worked, and to get 5, 10, 15, 20, 25 printed as you suggested you'd do this:

   def main():
      for i in range(1,6):
         print(MultiplyRecursive(i))

The functions here looks little over complicated, here the counting by fives version of mine.

def count_fives(n):
    if n > 0:
        count_fives(n-1)
        print(n*5)


count_fives(4)
"""Output:
5
10
15
20
"""

Recursive multiplication by addition:

def multiply(m,n):
    if n < 0:
        return -multiply(m, -n)
    elif n == 0:
        return 0
    else:
        return m + multiply(m, n-1)
def multiply(m, n):
    if n == 0 and m>0:
        while m>0:
            print(0)
            m -= 1
    elif m>0 and n != 0:
        while m>0:
            print(m * n)
            m -= 1
    elif m<=0:
        print('M must be greater than 0')

Does this work for what you are asking?

Does this work for what you are asking?

nichom your function is not recursive wish is the task here.
Look at pyTony soultion.

can someone please explain to me how the pytony's count_fives function works ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.