answersLogoWhite

0

The original answer really doesn't explain much, and almost nothing that's relevant to the question. (Sorry!) The part about iterations typically involving some kind of loop is on target, though it doesn't really shed much light.

This might best be explained by giving two pseudo-code examples, a function to calculate the factorial of an input argument. For simplicity's sake, since this is just to show examples, we will assume that we can be sure that the argument is a positive integer.

# This is an iterative function to calculate the factorial

Function I-Factorial(x)

Result = 1

For i = 1 to x

Result = Result * i

Next i

Return Result

End Function

# This is a recursive function to calculate the factorial

Function R-Factorial(x)

If x = 1 then

Return 1

Else

Return x * R-Factorial(x-1)

End If

End Function

Instead of stepping through a loop, the recursive function simply invokes itself, each invocation using an argument that's been reduced by 1 until ultimately it receives an argument of "1". Then the call for "R-Factorial(1)" is returned to the call for "R-Factorial(2)", which passes its result up to the call for "R-Factorial(3)", which passes its result up to the call for "R-Factorial(4)", and so on, until the accumulated result has bubbled its way up to the original call for "R-Factorial(x)".

As noted in the original answer, recursive functions can(but do not have to) incur additional overhead in terms of call stacks, memory allocations, and so on. If you write in assembly language (as I used to) and utilize reentrant coding techniques, the overhead is very close to zero.

=========

1. Recursion:

# When a recursive call is made, the method/process copies or clones itself, making new copy of:

* the code

* the local variables (with their initial values),

* the parameters

2. Iteration : there is no recursive call involved that saves a lot of time and space too as no extra space is needed to store each copy generated in recursion.

Iterative codes usually refers to codes that contain explicit iteration processes, that is, loops.

User Avatar

Wiki User

14y ago

What else can I help you with?

Continue Learning about Engineering

What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


The gross head in turbine installation is difference of level between?

it is difference between the water level from head race and tail race


What do you mean by base case recursive case binding time runtime stack and tail recursion?

These terms are found in Recursion.1.Base Case:it is the case in recursion where the answer is known,or we can say the termination condition for a recursion to unwind back.For example to find Factorial of num using recursion: int Fact(int num){ if(num==1 num==0)//base casereturn 1;else // recursive case: return num*Fact(num-1);} 2.Recursive case:It is the case whcih brings us to the closer answer. Run Time Stack:It is a system stack us to save the frame stack of a function every recursion or every call.This frame stack consists of the return address,local variables and return value if any. Tail Recursion:The case where the function consist of single recursive call and it is the last statement to be executed.A tail Recursion can be replace by iteration. The above function consists of tail recursion case.where as the below function does not. void binary(int start,int end,int el){int mid;if(end>start){mid=(start+end)/2;if(el==ar[mid])return mid;else{if(el>ar[mid])binary(mid+1,end,ele);elsebinary(start,mid-11,ele);


What is the use of recursion function?

Read the part in your programming manual/text book about recursion. The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.


When a function call by itself?

Recursion.

Related Questions

What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What are the differences between tail recursion and recursion, and how do they impact the efficiency and performance of algorithms?

Tail recursion is a special type of recursion where the recursive call is the last operation in the function. This allows for optimization by reusing the same stack frame for each recursive call, leading to better efficiency and performance. In contrast, regular recursion may require storing multiple stack frames, which can lead to higher memory usage and potentially slower execution.


Bring out the difference between recursion and iteration?

Recursion is repeatedly calling the function....---- whereas Iteration is nothing but just looping until condition doesn't satisfy.....


What is the answer for what is the difference between an ape and a monkey?

a ape has no tail and a monkey has a tail


How many types of recursion are there in c language?

Recursion in c language is a method where the function calls itself, within or outside the scope. Using Recursion, complicated problems can be divided into smaller parts so that solving them becomes more manageable. The recursion technique is available in Java, JavaScript, and C++.serves the same purpose. The type of Recursion in C • Direct Recursion • Indirect Recursion. Direct Recursion Recursion can call the function n-number of times. In the case of direct Recursion, the function calls itself inside the same position or in the local scope Direct Recursion problems are the Fibonacci series, a program to print 50 natural numbers. Indirect Recursion In the case of Indirect Recursion, a function X calls function Y, and function Y calls any function Z. Under certain conditions, function Z calls function A. In this case, function A is indirectly related to function Z. Indirect Recursion is also known as mutual Recursion, as more than one function runs a program. It is a two-step recursive function call process for making a recursive function call. Below mentioned are also type of Recursion: Tail Recursion No Tail/Head Recursion Linear Recursion Tree Recursion Tail Recursion A function is said to be tail recursion if it calls itself and also calls the last or the previous statement executed in the process. Head Recursion A function is said to be Head Recursion if it calls itself and also calls the first or the beginning statement executed in the process. Linear Recursion A function is said to be a linear recursive function if it makes a single call to itself each time the procedure executes itself and grows linearly depending on the size of the problem. Tree Recursion Tree Recursion is different from linear Recursion. Rather than making only one call to itself, that function makes more than one recursive call to the process within the recursive function. Following are the steps to solve the recursive problem in C: Step 1: Create a function and assign the work a part should do. Step 2: Select the subproblem and assume that the function already works on the problem. Step 3: Get the answer to the subproblem and use it to resolve the main issue. Step 4: The 90% of the problem defined is solved.


What is the difference between one-tailed and two-tailed test?

no tail


The gross head in turbine installation is difference of level between?

it is difference between the water level from head race and tail race


What is the difference between plain guppies and fancy tail guppies?

The only difference is the tail is about 1 cm bigger Males usually aready have fancy tails i find


What is the difference between a straight tailed pig and a curly trailed pig?

one has a curly tail and the other has a straight tail


What is the difference between variating tail lights on a Datsun Nissan 280zx?

well


What is the difference between mock tail and a cocktail?

A mocktail (or virgin cocktail) is nonalcoholic.


What do you mean by base case recursive case binding time runtime stack and tail recursion?

These terms are found in Recursion.1.Base Case:it is the case in recursion where the answer is known,or we can say the termination condition for a recursion to unwind back.For example to find Factorial of num using recursion: int Fact(int num){ if(num==1 num==0)//base casereturn 1;else // recursive case: return num*Fact(num-1);} 2.Recursive case:It is the case whcih brings us to the closer answer. Run Time Stack:It is a system stack us to save the frame stack of a function every recursion or every call.This frame stack consists of the return address,local variables and return value if any. Tail Recursion:The case where the function consist of single recursive call and it is the last statement to be executed.A tail Recursion can be replace by iteration. The above function consists of tail recursion case.where as the below function does not. void binary(int start,int end,int el){int mid;if(end>start){mid=(start+end)/2;if(el==ar[mid])return mid;else{if(el>ar[mid])binary(mid+1,end,ele);elsebinary(start,mid-11,ele);