Presented By:- RUPENDRA  CHOUDHARY B.Tech.- 2 nd  YEAR (CSE) E.C.B. ,BIKANER FUNCTION IN ‘C’
A function is a block of code that perform special task of some kind. WHAT IS FUNCTION?
WHY WE USE FUNCTION? main() { int i; printf(“computer science \n”); for(i=0;i<20;i++) { printf(“*”); } printf(“\n engineering hobby \n”); for(i=0;i<10;i++) { printf(“#”); } }
C:\function.c void function(int x,char y) { int i; for(i=0;i<x;i++) { printf(“%c”,y); } }
#include<c:\function.c> void main() { int i; printf(“computer science’s runner \n”); function(20,’*’); printf(“engineering is hobby \n”); function(10,’#’); }
PARTS OF FUNCTION 1)-Declaration 2)-Calling 3)-Definition
DESCRIPTION int sum(int,int); void main() { int a,b,add; scanf(“%d%d”,&a,&b); add=sum(a,b); printf(“add is %d”,add); } int sum(int x,int y) {  return x+y; }
FUNCTION TYPES 1)-No argument & No return value 2)-No argument & Having return value 3)-Having argument & No return value 4)-Having argument & Return value 5)-Recursive function
ILLUSTRATIONS 1)-What will be output? void main() { int i=3,r; r=sizeof f(++i)+f(--i)+f(i-2); printf(“%d%d”,r,i); } int f(int j) { if(!!j) return ++j; else return --j; }
2)-What will be output? main() { int a=5,b; { int b=10; ++b; ++a; printf(“%d%d\n”,a,b); { int a=20; ++a; a=++b; printf(“%d%d\n”,a,b); } ++a; ++b; Printf(“%d%d\n”,a,b); } Printf(“%d%d\n”,a,b); }
3)-What will be output ? func(int i) { if(i%2) return 0; else return 1; } main() { int i=1; i=func(i); i=func(i); printf(“%d”,i); }
4)- What will be output? int max(int x,int y) { return x>y?x:y; } void main() { int m; m=max(max(4,max(11,6)),max(10,5)); printf(“%d”,m); }
5) -What will be  output? int *fun(); void main() { int *j; j=fun(); printf(“%d”,*j); } int *fun() { int k=35; return &k; }
6)-what will be output? void increment(); void main() { increment(); increment(); } void increment() { auto int i=1; printf(“%d”,i); printf(“%d”,&i); }
7)-Will error occur? void display(); void main() { goto label; printf(“ecb alumni”); } void display() { label: printf(“computer science”); }
Concept of return value void swap(int,int);  void main()  { int a=1,b=2; swap(a,b); printf(“%d%d”,a,b); } void swap(int x,int y) { int  t; t=x; x=y; y=t; printf(“%d%d”,x,y); } void swap(int*,int*);  void main()  { int a=1,b=2; swap(&a,&b); printf(“%d%d”,a,b); } void swap(int *x,int *y) { int  t; t=*x; *x=*y; *y=t; }
Recursive function is a function which contains a call to itself RECURSIVE  FUNCTION
8)-What will be output? /* Based on stack */ void main() { int a=1,b=2,c=3,d; printf(“%d%d%d%d”); }
DESCRIPTION int fact(int); void main() { int a=4; result=fact(a); printf(“factorial vauue is=%d”,result); } int fact(int x) { if(x==1) return 1; else return x*fact(x-1); }
9)-Will error occur? void main() { int n; scanf(“%d”,n); printf(“%d”,fac()); } int fac(int n) { if(x==0) return 1; else return n*fac(n-1); }
10)-What will be output? int i=1; void main() { if(i<=3) { i++; main(); } printf(“%d%d%d”,i,++i,i++); }
RECURSION  V/S  ITERATION Recursive solution is always logical and it is very difficult to trace. Recursion takes a lot of stack space. Recursive calling increase the space complexity. A recursive definition defines an object in simpler cases of itself reducing nested looping complexity. Every recursive problem can be done by iteration but vice versa is not always true.
THANKS ANY QUERY….?

lets play with "c"..!!! :):)

  • 1.
    Presented By:- RUPENDRA CHOUDHARY B.Tech.- 2 nd YEAR (CSE) E.C.B. ,BIKANER FUNCTION IN ‘C’
  • 2.
    A function isa block of code that perform special task of some kind. WHAT IS FUNCTION?
  • 3.
    WHY WE USEFUNCTION? main() { int i; printf(“computer science \n”); for(i=0;i<20;i++) { printf(“*”); } printf(“\n engineering hobby \n”); for(i=0;i<10;i++) { printf(“#”); } }
  • 4.
    C:\function.c void function(intx,char y) { int i; for(i=0;i<x;i++) { printf(“%c”,y); } }
  • 5.
    #include<c:\function.c> void main(){ int i; printf(“computer science’s runner \n”); function(20,’*’); printf(“engineering is hobby \n”); function(10,’#’); }
  • 6.
    PARTS OF FUNCTION1)-Declaration 2)-Calling 3)-Definition
  • 7.
    DESCRIPTION int sum(int,int);void main() { int a,b,add; scanf(“%d%d”,&a,&b); add=sum(a,b); printf(“add is %d”,add); } int sum(int x,int y) { return x+y; }
  • 8.
    FUNCTION TYPES 1)-Noargument & No return value 2)-No argument & Having return value 3)-Having argument & No return value 4)-Having argument & Return value 5)-Recursive function
  • 9.
    ILLUSTRATIONS 1)-What willbe output? void main() { int i=3,r; r=sizeof f(++i)+f(--i)+f(i-2); printf(“%d%d”,r,i); } int f(int j) { if(!!j) return ++j; else return --j; }
  • 10.
    2)-What will beoutput? main() { int a=5,b; { int b=10; ++b; ++a; printf(“%d%d\n”,a,b); { int a=20; ++a; a=++b; printf(“%d%d\n”,a,b); } ++a; ++b; Printf(“%d%d\n”,a,b); } Printf(“%d%d\n”,a,b); }
  • 11.
    3)-What will beoutput ? func(int i) { if(i%2) return 0; else return 1; } main() { int i=1; i=func(i); i=func(i); printf(“%d”,i); }
  • 12.
    4)- What willbe output? int max(int x,int y) { return x>y?x:y; } void main() { int m; m=max(max(4,max(11,6)),max(10,5)); printf(“%d”,m); }
  • 13.
    5) -What willbe output? int *fun(); void main() { int *j; j=fun(); printf(“%d”,*j); } int *fun() { int k=35; return &k; }
  • 14.
    6)-what will beoutput? void increment(); void main() { increment(); increment(); } void increment() { auto int i=1; printf(“%d”,i); printf(“%d”,&i); }
  • 15.
    7)-Will error occur?void display(); void main() { goto label; printf(“ecb alumni”); } void display() { label: printf(“computer science”); }
  • 16.
    Concept of returnvalue void swap(int,int); void main() { int a=1,b=2; swap(a,b); printf(“%d%d”,a,b); } void swap(int x,int y) { int t; t=x; x=y; y=t; printf(“%d%d”,x,y); } void swap(int*,int*); void main() { int a=1,b=2; swap(&a,&b); printf(“%d%d”,a,b); } void swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; }
  • 17.
    Recursive function isa function which contains a call to itself RECURSIVE FUNCTION
  • 18.
    8)-What will beoutput? /* Based on stack */ void main() { int a=1,b=2,c=3,d; printf(“%d%d%d%d”); }
  • 19.
    DESCRIPTION int fact(int);void main() { int a=4; result=fact(a); printf(“factorial vauue is=%d”,result); } int fact(int x) { if(x==1) return 1; else return x*fact(x-1); }
  • 20.
    9)-Will error occur?void main() { int n; scanf(“%d”,n); printf(“%d”,fac()); } int fac(int n) { if(x==0) return 1; else return n*fac(n-1); }
  • 21.
    10)-What will beoutput? int i=1; void main() { if(i<=3) { i++; main(); } printf(“%d%d%d”,i,++i,i++); }
  • 22.
    RECURSION V/S ITERATION Recursive solution is always logical and it is very difficult to trace. Recursion takes a lot of stack space. Recursive calling increase the space complexity. A recursive definition defines an object in simpler cases of itself reducing nested looping complexity. Every recursive problem can be done by iteration but vice versa is not always true.
  • 23.