Professional Documents
Culture Documents
d90e7ab0-d98a-4ad8-918b-39d3f73898f0
d90e7ab0-d98a-4ad8-918b-39d3f73898f0
Note:- Reason for the unexpected output is, main function is calling swap function as "call by value".
=>when the function call is call by value, changes made through
formal parameters do not get reflected in actual parameters.
1 #include<stdio.h>
2 void swap(int *,int *);
3 main()
4{
5 int n1,n2;
6 n1=10;
7 n2=20;
8 swap(&n1,&n2);//call by reference
9 printf("After swapping n1=%d and n2=%d",n1,n2);
10}
11void swap(int *a,int *b)
12{
13 int c;
14 c=*a;
15 *a=*b;
16 *b=c;
17 }
Q)What is recursion?
=>A function calling itself is known as recursion.
=>Certain computing problems can be solved efficiently using recursion.
For eg. finding the factorial of a number,finding the Fibonacci series,
traversing a tree(data structure) etc.
=>Programmer has to be careful while implementing recursion.An exit
condition must be defined for the recursive calls of the function.Otherwise,
it will go into an infinite loop.
//factorial.c
1 #include<stdio.h>
2 long int findfactorial(int);
3 main()
4{
5 int n;
6 long int factorial;
7 printf("Enter the number:");
8 scanf("%d",&n);
9 factorial=findfactorial(n);
10 printf("Factorial of %d is %d",n,factorial);
11}
12 long int findfactorial(int i)
13{
14 long int factorial;
15 if(i==0)
16 return 1;
17 else
18 return i*findfactorial(i-1);
19 }