473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

when to use recursion and is it better than a function ...in c?

which is better recursion or function ....???and when to use function ?and when to use recursion?
Oct 9 '09 #1
3 3451
Banfa
9,065 Expert Mod 8TB
Your question is unclear, a function is a language construct where as recursion is a programming technique (normally achieved using a function that can be called recursively), they are not comparable.

Did you mean which is better recursion or iteration?

You normally have better control of memory usage, particularly stack usage with iteration. On the other hand recursion often produces more elegant, and therefore more easy to maintain code.
Oct 9 '09 #2
myusernotyours
188 100+
Some problems also will lend themselves nicely to recursion. What you should avoid is a situation where you use recursion in an unintuitive manner. It becomes hard to read.
I also find that some situations are so naturally recursive that you don't even need to think deeply about all the paths at all but the end result is very elegant code if you check it out keenly.

Regards,

Alex.
Oct 9 '09 #3
donbock
2,426 Expert 2GB
The only simple and universal answer to which is better, recursion or iteration, is ... it depends.

Recursion sometimes, but not always, yields simpler and more elegant source code. Simpler and more elegant source code is usually easier to review and easier to maintain. It may have a smaller footprint in executable memory. Each recursive invocation gets its own set of automatic variables, so the invocations are protected from unintended interactions with other invocations.

Iteration almost always uses less stack space and may run a little faster for nominal conditions. All iterations have equal access to the function's automatic variables so it is easier to make state information available to all iterations.

Which is better depends on your situation. For example, an embedded application may not be able to tolerate the possibly unbounded stack usage of a recursive function.
Oct 9 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
11
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
10
by: randomtalk | last post by:
hello, i have another problem i feel that i have to be missing something.. Basically, i've written a recursive function to find all the prime up to a number (lim).. here is the function: The...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
20
by: athar.mirchi | last post by:
..plz define it.
10
by: slix | last post by:
Recursion is awesome for writing some functions, like searching trees etc but wow how can it be THAT much slower for computing fibonacci- numbers? is the recursive definition counting fib 1 to...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.