0

I am currently learning Python and would like some clarification on the difference between iterative and recursive functions. I understand that recursive functions call themselves but I am not exactly sure how to define an iterative function.

For instance, I wrote this code

random_list = ['6', 'hello', '10', 'find', '7']

def sum_digits(string):
    return sum(int(x) for x in string if x.isdigit())

print "Digits:", sum_digits(random_list)

I thought that this was an iterative function but after doing some research I am not sure. I need to know specifically because the next exercise asks me to write a version of the function that is recursive/iterative (depending on what my first function is).

4
  • 4
    What makes you think this isn't an iterative function? Commented Aug 23, 2018 at 21:13
  • A recursive function F calls itself, either directly or by calling some other functions that eventually call F. Everything else is an iterative function. Simple as that. Commented Aug 23, 2018 at 21:33
  • It calling itself is what threw me off. Commented Aug 23, 2018 at 21:36
  • It is not calling itself Commented Aug 24, 2018 at 7:14

4 Answers 4

2

Recursive function call itself while does not reach the out poin whereas iterative function update calculating value through the iteration over the range.

Sign up to request clarification or add additional context in comments.

Comments

2

To those who might still want to see the difference between recursive and iterative function.

iterative

def iterative_sum(n):
    result = 1
    for i in range(2,n+1):
        result *= i
    return result
print(iterative_sum(5))

iteration is when a loop repeatedly executes until the controlling condition becomes false

recursive

def recursive_sum(n):
    if n == 1:
        return 1
    else:
        return n * recursive_sum(n-1)
print(recursive_sum(5))

recursive function is when a function calls itself

This link explains it much better https://techdifferences.com/difference-between-recursion-and-iteration-2.html

Comments

1

so the question is "write an iterative and recursive version of sum". great.

don't use the built-in sum method, and write your own. i'll give you the iterative, you should figure out the recursive:

def my_iterative_sum(a):
    res = 0
    for n in a:
        res += a
    return res

this is iterative because it iterates over all the values and sums them up.

edit: clearly your post is iterative. are you calling function f from within function f? no.

maybe reading up on what recursion is will help with this. https://www.google.com/search?q=recursion

8 Comments

I guess it's worth pointing out that writing a stable recursive sum in Python is not an easy fit for a beginner, especially if one doesn't not what recursion is.
I thought that a recursive function called itself which is why I thought it wasn't iterative even though I know that it was repeating itself over every item. But thanks!
i know what you're saying, but where in your example is there a function calling itself?
Looking at it again after sleeping makes me wonder why I thought that to be honest. I can see that it is clearly iterative now haha. Thanks again for your help
hey, glad you got it sorted! were you able to implement the recursive func?
|
0

In case you came here looking for recursive functions in Python, here is an example of a recursive sum

from typing import Optional

>>> def r_sum(v: int, sum: Optional[int] = None) -> int:
...   if sum is None:
...     sum = 0
...   if v == 0:
...     return sum
...   else:
...     sum = sum + v
...     prev = v - 1
...     return r_sum(prev, sum)
... 
>>> r_sum(3)
6
>>> r_sum(4)
10
>>> r_sum(5)
15

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.