Function, Scope and Recursion
Function, Scope and Recursion
FUNCTION
Q.1 Add two nos using function
#include <stdio.h>
void add(float a1, float b1)
{
float res;
res=a1+b1;
printf("Result = %f",res);
}
int main()
{
float a,b,c;
printf("\n Enter two nos");
scanf("%f %f",&a,&b);
add(a,b);
return 0;
}
Q.2 Add two nos using function and return result back to main and print
#include <stdio.h>
void add()
{
Int c;
c=a+b;
printf("Result = %f",c);
}
int main()
{
float a,b,c;
printf("\n Enter two nos");
scanf("%f %f",&a,&b);
add(a,b);
return 0;
}
Q.3 Add two nos using function and return result back to main and print (function
protype)
#include <stdio.h>
int add(int a,int b); // or int add(int,int); function declaration or function prototype
int main()
{
int a,b,res;
printf("enter 2 nos");
scanf("%d %d", &a,&b);
res=add(a,b); // function calling
printf("\n result is : %d",res);
return 0;
}
int add(int a,int b) // function defination
{
printf("\n Welcome to function add");
int total;
total=a+b;
return total;
}
Q.4 Odd or even using function and return result back (option 1)
#include <stdio.h>
int evenorodd(int a); // or int add(int,int); function declaration or function prototype
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
res=evenorodd(a); // function calling
if(res==0)printf ("\n even");
else printf ("\n odd");
return 0;
}
int evenorodd(int a) // function defination
{
printf("\n Welcome to function add");
if (a%2==0) return 0;
else return 1;
}
Q.4 Odd or Even using function and return result back (option 2)
#include <stdio.h>
int evenorodd(int a); // or int add(int,int); function declaration or function prototype
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
res=evenorodd(a); // function calling
if(res==0)printf ("\n even");
else printf ("\n odd");
return 0;
}
int evenorodd(int a) // function defination
{
printf("\n Welcome to function add");
return(a%2);
}
Q.4 Odd or Even using function and return result back (option 3)
#include <stdio.h>
int evenorodd(int a); // or int add(int,int); function declaration or function prototype
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
if(evenorodd(a)==0) // function calling
printf ("\n even");
else printf ("\n odd");
return 0;
}
int main()
{
int a;
printf("enter 1 no");
scanf("%d", &a);
p(a);
return 0;
}
Q.6 Checking a number is prime using function and return the result bac to the main
(option 2)
#include <stdio.h>
int p(int a) // function defination
{
int flag=1, i;
for(i=2;i<a;i++)
{
if(a%i==0)
{
flag=0;
break;
}
}
return flag;
}
int main()
{
int a;
printf("enter 1 no");
scanf("%d", &a);
if(p(a)) printf("\n Prime");
else printf("\n non prime");
return 0;
}
int main()
{
int a;
printf("enter 1 no");
scanf("%d", &a);
factors(a);
return 0;
}
Q.8 Calculate power using function
#include <stdio.h>
void powercalc(int, int);
int main()
{
int base, power;
printf(" Enter base and power");
scanf("%d %d",&base,&power);
powercalc(base, power);
return 0;
}
int main()
{
int num;
printf("\n Enter the number :");
scanf("%d",&num);
palin(num);
return 0;
}
}
Q. 9 User defined function calling another function- Palindrome (option 2)
#include <stdio.h>
void palin(int num);
void input ();
int main()
{
input();
return 0;
}
void input ()
{
int num;
printf("\n Enter the number :");
scanf("%d",&num);
palin(num);
}
void palin(int num)
{
int rev=0,m=num;
while(num>0)
{
rev=rev*10+num%10;
num=num/10;
}
printf("\n Reverse is : %d",rev);
if(m==rev)
printf("\n Its a palindrom");
else
printf("\n It is not a palindrom");
}
return 0;
}
Q.11 Factors of a no
#include <stdio.h>
void factors(int); // fun declaration / protype
int main()
{
int num;
printf("Enter a no");
scanf("%d",&num);
factors(num); // fun calling
return 0;
}
}
Hello world is printed only infinite times since the variable i is declared new after every function
call
Q. Recursion Example
#include <stdio.h>
void count_to_ten ( int count )
{
//we only keep counting if we have a value less than ten
if ( count < 10 )
{
count_to_ten( count + 1 );
printf(" \n %d",count);
}
}
int main()
{
count_to_ten ( 0 );
}
Print 9…0
Another example
#include <stdio.h>
void count_to_ten ( int count )
{
//we only keep counting if we have a value less than ten
if ( count < 10 )
{
printf(" \n %d",count);
count_to_ten( count + 1 );
}
}
int main()
{
count_to_ten ( 0 );
}
Print 0…9
Q Static variable
#include <stdio.h>
void main()
{
static int i=1;
printf("Hello World");
i++;
if(i<=5)
main();
}
Hello world is printed only 5 times since the variable is static (it retains its value)
Q.1 Global variable
#include <stdio.h>
int f=10;
void fun();
int main()
{
printf(" %d",f++);
fun();
return 0;
}
void fun()
{
printf(" %d",f);
}
OUTPUT: 10 11
Q.2 Global variable vs local variable
#include <stdio.h>
int f=10;
void fun();
int main()
{
int f=5;
printf(" %d",f++);
fun();
return 0;
}
void fun()
{
printf(" %d",f);
}
OUTPUT: 5 10