I want to know when it is necessary to include a return statemnt in one's code and also what is the difference between recursion and iteration?

Recommended Answers

All 10 Replies

>when it is necessary to include a return statemnt in one's code
When you're returning from any function whose return type is not void.

>what is the difference between recursion and iteration?
At what level? At the highest level, there's no difference. Go a bit lower and recursion is just a more flexible form of iteration. Go down to the client level and recursion is a series of nested function calls while iteration is just a glorified goto loop. Go even deeper and recursion is an execution stack where stack frames are piled on top of each other and the instructions that operate on each frame happen to be the same while iteration is nothing more than conditionally jumping around the same instructions within one stack frame.

A loop can repeat a series of instructions only if each iteration immediately follows the previous one right? But in case of recursive functions, you can call the function at any point of the program and the series of instructions will be executed right? So can you say that the point I made is the difference between them?

>>you can call the function at any point of the program
that may, or may not be recursion. recursion happens only when the function is called somewhere within the same function. For example

void foo()
{
<snip>
    foo();
}

>So can you say that the point I made is the difference between them?
No. I don't see how the two statements describe a difference unless you're adding the restriction that only one loop can be used ever and it can't be placed in a function.

>>only one loop can be used ever and it can't be placed in a function.
I didn't understand what you meant by that.Why can't it be placed in a function?

>Why can't it be placed in a function?
It can. For your statement to be true, both of my conditions would have to be true, and neither of them are.

So what would one cite as the main difference(if any) between iteration and recursion?

are these following points differences?
Repition
iteration:explicit loop
recursion:repeated function calls

Termination
Iteration:when condition fails
Recursion:when base case is recognized

>So what would one cite as the main difference(if any) between iteration and recursion?
Recursion is recursive and iteration just repeats.

>are these following points differences?
The first is, but the second is just two ways of saying the same thing.

Alright! Thanks :)

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.