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

Post on 20-Jan-2015

573 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

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 rupendrachoudhary1990@gmail.com or https://rupendrachoudhary.wordpress.com

TRANSCRIPT

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’

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?

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

}

}

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

}

}

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,’#’);

}

Copyright © 2001 Stephen A. Edwards All rights reserved

PARTS OF FUNCTIONPARTS OF FUNCTION

1)-Declaration

2)-Calling

3)-Definition

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;

}

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

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

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

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

}

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

}

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;

}

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

}

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”);

}

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;

}

Copyright © 2001 Stephen A. Edwards All rights reserved

Recursive function is a function which contains a call to itself

RECURSIVE FUNCTION RECURSIVE FUNCTION

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”);

}

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

}

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

}

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++);

}

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.

Copyright © 2001 Stephen A. Edwards All rights reserved

THANKSTHANKS

ANY QUERY….?

top related