TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What is the difference between iterative and recursive functions?

2 pointsby _pcpeabout 14 years ago
I said everything could be done by iterative way but can not be done through recursive where my friend said the exact opposite way. Not sure which one is true.

2 comments

chalstabout 14 years ago
You can design languages that have only iteration, such as Dijkstra's guarded command language extended with data types, or have only recursion, such as the lambda calculus, and each can code up the other as an interpreter. So there is a basic sense in which neither is more expressive than the other.<p>But there is a sense in which recursion is more expressive than iteration, in the sense of being a flexible local abstraction: in a language with tail recursion, iterative loops can be directly encoded by means of recursive calls. And indeed this is how iteration is expressed in the Scheme language. Trying to encode recursion in terms of iteration cannot be done so directly.
muyyatinabout 14 years ago
Depends exactly on what you mean by iterative and recursive functions.<p>Summing a list could be done by:<p>sum_iterative( list ) { sum = 0 for( element in list ) { sum += element } return sum }<p>or<p>sum_recursive( list ) { if( list.empty ) { return 0 } else { return list.head + sum_recursive( list.tail ) } }<p>So saying everything that can be done iteratively can't be done recursively is false, and also vice versa.