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

23
Copyright © 2001 Stephen A. Edwards All rights reserved Presented By:- RUPENDRA CHOUDHARY B.Tech.- 2 nd YEAR (CSE) E.C.B. ,BIKANER FUNCTION IN ‘C’

description

hey this is Rupendra choudhary..!! i shared my "c" lang ppt..!!! u just goto that ppt if u r in deep with "c" ..!!! i create after i hv played a much with "c"..(sorry bt ppt is slightly disturbd may be due to unsupportable msppt2010 by slideshare)...find me on [email protected] or https://rupendrachoudhary.wordpress.com

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

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

Copyright © 2001 Stephen A. Edwards All rights reserved

Presented By:-

RUPENDRA CHOUDHARY

B.Tech.- 2nd YEAR (CSE)

E.C.B. ,BIKANER

FUNCTION IN ‘C’ FUNCTION IN ‘C’

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

Copyright © 2001 Stephen A. Edwards All rights reserved

A function is a block of code that perform special task of some kind.

WHAT IS FUNCTION? WHAT IS FUNCTION?

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

Copyright © 2001 Stephen A. Edwards All rights reserved

WHY WE USE 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(“#”);

}

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

C:\function.c C:\function.c

void function(int x,char y)

{

int i;

for(i=0;i<x;i++)

{

printf(“%c”,y);

}

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

#include<c:\function.c>

void main()

{

int i;

printf(“computer science’s runner \n”);

function(20,’*’);

printf(“engineering is hobby \n”);

function(10,’#’);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

PARTS OF FUNCTIONPARTS OF FUNCTION

1)-Declaration

2)-Calling

3)-Definition

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

Copyright © 2001 Stephen A. Edwards All rights reserved

DESCRIPTION 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;

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

FUNCTION TYPES 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

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

Copyright © 2001 Stephen A. Edwards All rights reserved

ILLUSTRATIONS 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; }

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

Copyright © 2001 Stephen A. Edwards All rights reserved

2)-What will be output? 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);}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

3)-What will be output ?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);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

4)- What will be output? 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);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

5) -What will be output? 5) -What will be output?int *fun();

void main()

{

int *j;

j=fun();

printf(“%d”,*j);

}

int *fun()

{

int k=35;

return &k;

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

6)-what will be output? 6)-what will be output?void increment();

void main()

{

increment();

increment();

}

void increment()

{

auto int i=1;

printf(“%d”,i);

printf(“%d”,&i);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

7)-Will error occur?7)-Will error occur?void display();

void main()

{

goto label;

printf(“ecb alumni”);

}

void display()

{

label:

printf(“computer science”);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

Concept of return value Concept of return valuevoid 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;

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

Recursive function is a function which contains a call to itself

RECURSIVE FUNCTION RECURSIVE FUNCTION

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

Copyright © 2001 Stephen A. Edwards All rights reserved

8)-What will be output? 8)-What will be output?

/* Based on stack */

void main()

{

int a=1,b=2,c=3,d;

printf(“%d%d%d%d”);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

DESCRIPTION DESCRIPTIONint 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);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

9)-Will error occur?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);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

10)-What will be output? 10)-What will be output?

int i=1;

void main()

{

if(i<=3)

{

i++;

main();

}

printf(“%d%d%d”,i,++i,i++);

}

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

Copyright © 2001 Stephen A. Edwards All rights reserved

RECURSION V/S ITERATION 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.

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

Copyright © 2001 Stephen A. Edwards All rights reserved

THANKSTHANKS

ANY QUERY….?