360 likes | 595 Views
Chinese Proverb says……. A Person who asks Questions will remain fool for few seconds but one who never asks remains fool for the entire life . Advantages. void main( ) { int n , k , i ; printf (“<br> Enter number:-”); scanf (“%d”, &n); for( i =2 ; i <=(n/2) ; i ++ ){
E N D
Chinese Proverb says…….. A Person who asks Questions will remain fool for few seconds but one who never asks remains fool for the entire life .
Advantages void main( ) { int n , k , i ; printf(“\n Enter number:-”); scanf(“%d”, &n); for(i=2 ; i <=(n/2) ; i++ ){ if ( n % i = =0 ) { k=1; } // if ends } // for ends if ( k = = 1 ){ printf(“Not Prime”); } else{ printf (“ Prime ” );} } // main ends
Advantages int prime (int); intAskFromUser( ); void display( ); void main( ) { int n , k , i ; n=AskFromUser( ); k=prime(n); display(k); } OR void main( ){ display ( prime( AskFromUser( ))); }
Advantages intprime( int n){ int flag=0,i; for(i=2 ; i <=(n/2) ; i++ ){ if ( n % i = = 0 ) { flag=1; } return(flag); } intAskFromUser( ){ int n; Printf(“\nEnter A number:”); Scanf(“%d”,&n); }
Advantages void display(int k) { if (k==1) printf(“Not Prime”); else printf(“Prime”); }
Advantages Reduces Complexity & increases Readability Makes Program size smaller Debugging becomes very easy can be reused in other programs
Types of Functions Built in or Library Eg. Printf( ) , Scanf ( ) , etc User Defined Eg. Prime( ) , fibonaci( ) , etc
Questions ? What is the Type of main ( ) ? What type of language is C ? and why ? How many types of datatype classes r there in C ? What r keywords ? Functions names are stored in the same way as variables .
Brain Teaser 1 func( ); main( ) { printf( “ %d” , func); } func( ) { return (0); }
Brain Teaser 2 main( ) { printf( “ %u” , main( ) ); }
Three main things….. • Function Declaration ( prototype) • Function call • Function Definition
Types of Arguments(Parameters) • Formal Arguments • Actual Arguments Eg : • Call func ( 10 ,20 ,30 , 40); • Defi. func (int a, int b ,int c ,int d );
ANSI Method • void main(){ • Func(10,20); • } • Func(inta,int b) • { • Printf (“ Sum=%d ”,(a+b)); • }
K & R Method • void main(){ • Func(10,20); • } • Func(a,b) • int a; • int b; • { • printf (“ Sum= %d ”,(a+b)); • }
How Arguments r passed ? In TC , the argument calling convention is from right to left For Eg: func(10,20,30,40); // function call func( int a , int b ,int c , int d) // function Defi. { ……..some code…… }
How Arguments r passed ? void func(int , int ,int , int ); void main( ) { int a=5; func( a , a++ , ++a , --a ); } void func(int a, int b ,int c ,int d ) { printf(“ %d %d %d %d ”, a, b , c , d ); }
Call By Value & Call By Reference void main( ){ intfunc(int ) ; int j=10 , i ; i=fun( j ); printf (“ %d %d ”,--i , j ); } int fun(int j){ return ( j++ ); }
Recursion void main ( ) { inti=0; func(i ); } void func(inti ){ i++; printf(“%d” , i ); if ( i!=5 ) func( i); }
Recursion void main ( ) { inti=0; func(i ); } void func(inti ){ i++; if ( i!=5 ) func( i); printf(“%d” , i); }
Question ? • What is required in Recursion ? • What is better Recursion or using loops ?
Scope (active), Visibility& Longetivity ( alive) inti=10; void main( ){ Func( ); Printf ( “%d ”, i ); } // main ends Func( ){ inti=20; // scope of this i is only inside this // func . Global i is not visible here // but it is alive Printf(“ %d ” , i); }
Type of Variables • Automatic ( local or internal) • Static (can be local or external) • Register • Extern • Global ( external )
Automatic Variable • By Default • Given Garbage value if uninitialized • They r local
Brain Teaser void main(){ int a; Func(a); Printf(“%d ”, a); } Func(int a) { a=++a || a++ || a-- && --a; return(a); }
Static Variable • Retains its value during function calls • Can be local or external • Difference between global varaible and external static variable • Gets default value =0 • A func can also be static
Brain Teaser void main( ){ inti; for(i=0;i<3;i++){ Func( ); } Func( ){ Static inti=0; i++; Print f(“%d” , i); }
Register Variable Faster Access • Loop Control variables • Cant be pointed by some pointers
Brain Teaser void main( ) { register inti=10; int *p; p=&i; p=(*p)++; printf (“%d”,*p); }
Extern Variable #include<stdio.h> void main( ) { extern int x; printf( “ %d ”, x ); } int x=10;
Extern Variable #include<stdio.h> void main( ) { extern int x; printf( “ %d ”, x ); }
A function cannot be the target of an assignment • You cannot define func inside another function .
Brain Teaser int a; void main( ) { Func( )=10; printf( “ %d ” , a); } Func( ) { return (a); }
Brain Teaser void main( ) { void func( ) • { • printf( “ Hello C ”); • } func(); }
Function returning Pointers • int *func(); • void main(){ • inti,*p; • p=func(); • } • int *func(){ • int x=10,*q; • q=&x; • return (q); • }
Pointer To Function • void func( ); • void main(){ • int (*p)( ); • p=func; • (*p)( ); • } • void func( ){ • Printf(“Hello LD”); • }
Brain Teaser • int *func( ); • void main(){ • int *p; • p=func(); • printf(“\n”); • printf( “ %d ” , *p); • } • int *func( ){ • int k=35; • return ( &k); • }