0% found this document useful (0 votes)
4 views9 pages

C Programming: Recursion vs Iteration

The document discusses programming methodology in C, highlighting that it is not a block-structured language like Pascal, but allows variable declarations within functions that can hide outer variables. It explains recursion as a divide and conquer approach to problem-solving, emphasizing the importance of base cases and state changes. Additionally, it compares recursion and iteration, noting their differences in memory usage, code length, and ease of understanding.

Uploaded by

letsjustuseai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

C Programming: Recursion vs Iteration

The document discusses programming methodology in C, highlighting that it is not a block-structured language like Pascal, but allows variable declarations within functions that can hide outer variables. It explains recursion as a divide and conquer approach to problem-solving, emphasizing the importance of base cases and state changes. Additionally, it compares recursion and iteration, noting their differences in memory usage, code length, and ease of understanding.

Uploaded by

letsjustuseai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming Methodology

Block structure
• C – is not a block structured language like pascal or similar languages
because of functions may not be defined within other functions.
• Variable can be defined in a block structured fashion within functions.
• Declarations of variables may follow left brace that introduces any
compound statement.
• Variables declared in this way hide any identically named variables in
outer blocks and remain in existence until the matching right brace is
found.
• For example
if(n>0)
{
int i;
for(i=0;i<n;i++){

}
}
#include <stdio.h>
int a(int b){
int s=0;
if(b>5)
{
int i;
for(i=1;i<=5;i++)
s=s+i;
}
return s;
}
int main()
{
int b=10;
int res;
res=a(b);
printf("%d", res);
return 0;
Recursion – Divide and Conquer Approach
• Strategy to solve problems
• Break problem into smaller problems.
• Solve smaller problems.
• Combine results.
• Strategy can be applied “recursively” to smaller problems.
• Continue dividing problem until solution is trivial.
• Figure out when to stop dividing the problem.
Recursion
• C functions may be used recursively.
• Function calls itself either directly or indirectly.
• When a function calls itself recursively, each invocation gets a fresh set of all the automatic variables,
independent of the previos set.
int Factorial(int n)
{
if(n>0)
return n* Factorial(n-1);
else
return 1;
}

Write a program to find Fibonacci series using recursion.


Factorial(0) //1
1*factorial(0)
2*factorial(1)
3*factorial(2)
Rules on writing recursive solutions
• Always have some base cases, which can be solved without recursion
• Must change its state and move toward the base case.
• Must call itself, recursively.
• Some trivial version of the problem that is easily solved without using recursion.
Recursion vs Iteration
• Both repeatedly executes the same instructions.
• Recursion is a process, always applied to a function.
• Recursion terminates when a base case is recognized.
• Recursion is when a statement in a function calls itself.
• Recursion uses stack.
• Recursion uses more memory than iteration.
• Recursion makes the code smaller.
• Recursion is sometimes easier to understand.
• Iteration is applied to the set of instructions.
• Iterations is a loop repeatedly executes until the controlling condition becomes false.
• Iteration never uses stack.
• Iteration consumes less memory.
• Iteration makes the code longer.

You might also like