Recursion:
Executing a statement or multiple statements repeatedly is
called iteration or loop.
A loop can be build either by using any iterative control
structure or recursion.
Recursion is executing a statement or multiple statements
repeatedly by calling a function on itself.
To solve simple problems iterative control structure is
better comparatively with recursion.
Some times complex problems may be easily solved using
recursion.
Example1:
Output:
Hello world…………….
Note: Use ctrl+break
to stop the program.
Example2:
Output:
1 2 3………..
Note: Use ctrl+break
to stop the program.
Example3:
#include<stdio.h>
int x=1;
void main()
{
printf("\t%d",x);
x++;
if(x<=5)
main();
getch();
}
Output:
1 2 3 4 5
Specification1:
Print the sum of natural numbers using recursion.
Program:
#include<stdio.h>
int sum(int);
void main()
{
int n,sm;
clrscr();
printf("Enter
the limit:");
scanf("%d",&n);
sm=sum(n);
printf("Sum
%d",sm);
getch();
}
int sum(int n)
{
int s;
if(n==1)
return 1;
s=n+sum(n-1);
return s;
}
Execution:
Enter the limit: 5
Sum 15
Example explained:
Here the sum function calls on itself.
Specification2:
Accept an integer and print its factorial using recursion.
Program:
#include<stdio.h>
long int factorial(int);
void main()
{
int n;
long int fact;
clrscr();
printf("Enter
any integer:");
scanf("%d",&n);
fact=factorial(n);
printf("Factorial %ld",fact);
getch();
}
long int factorial(int n)
{
long int f;
if(n==1)
return 1;
f=n*factorial(n-1);
return f;
}
Execution:
Enter any integer: 8
40320
Visibility and scope of variables:
Visibility is how far a variable is accessible.
Scope is the life of variable that is when is allocated and
de-allocated.
According to the visibility and scope variables are
classified into
- Local
variables
- Global
variables
- Block
variables.
Local variables:
Variables declared with in a function are called local
variables.
The visibility of local variables is limited to the function.
Local variables belongs to one function can't be accessed
into another function.
Scope (life) of local variables is limited to the function.
Memory allocation of local variables allocates at the
beginning of function execution and de-allocates at the end of function
execution.
Example1:
/* visibility of local variable */
#include<stdio.h>
void main()
{
int x=10;
clrscr();
display();
getch();
}
void display()
{
printf("x=%d",x);
}
Output:
Error: undefined symbol "x" in function display()
Example explained:
"x" is the local variable to main(), can't be
accessed into display() because the visibility of "x" is limited to the
main() function.
Example2:
/* visibility of local variable */
#include<stdio.h>
void main()
{
int x=40;
clrscr();
printf("x=%d",x);
process();
printf("\nx=%d",x);
getch();
}
void process()
{
int x=80;
printf("\nx=%d",x);
}
Output:
x=40
x=80
x=40
Example explained:
"x" is declared for two times that is in main()
and process().
They are different; visibility of which is limited to their
home functions.
Example3:
/* scope of local variables */
void display(int);
void main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
display(i);
getch();
}
void display(int x)
{
int a=100;
a=a+x;
printf("\t%d",a);
}
Output:
101 102 103 104 105
Example explained:
Because "a" is a local variable to display(),
memory allocation of which is allocated at the beginning of function execution
and de-allocates on completion of function execution.
The value of "a" is not preserved for next
iterations.
0 comments:
Post a Comment