Rules of Writing Function : C Programming
Rules of Writing Function in C Programming
Rule 1. C program is a collection of one or more functions
main() { pune(); mumbai(); maharashtra(); }
Rule 2. A function gets called when the function name is followed by a semicolon.
For example :
main( ) { display( ) ; }
Rule 3 : A function is defined when function name is followed by a pair of braces in which one or more statements may be present.
For example,
display( ) { statement 1 ; statement 2 ; statement 3 ; }
Rule 4. Any function can be called from any other function.( main also )
main( ) { message( ) ; } message( ) { printf ( "\nWe are going to call main" ) ; main( ) ; }
Rule 5.A function can be called any number of times.
For example,
main() { message( ); message( ); } message() { printf("\nLearning C is very Easy"); }
Rule 6. The order in which the functions are defined in a program and the order in which they get called need not necessarily be same
main( ) { message1( ) ; message2( ) ; } message2( ) { printf ( "\n I am First Defined But Called Later " ) ; } message1( ) { printf ( "\n I am Defined Later But Called First ") ; }
Rule 7. A function can call itself ( Process of Recursion )
int fact(int n) { if(n==0) return(1); return(n*fact(n-1)); }