Function is block or part of program. When any program is very long or same code is repeating many times then we try to cut the program in different parts (or blocks) so that whole program became more understandable, easier to debug (error checking) and size of code will be lesser.
Simple example of function structure
int sum (int,int); //function declaration
int main(){
int p;
p=sum(3,4); //function call
printf(“%d”,sum);
return 0;
return 0;
}
int sum( int a,int b){ //function definition
int s; //function body
s=a+b;
return s; //function returning a value
}
Detail explanation of syntax of function
(1) Function_name :
Function naming rule in c programming:
Rule 1. Name of function includes alphabets, digit and underscore.
Valid name: world, addition23, sum_of_number etc.
Invalid name: factorial#, avg value, display*number etc.
More example click here
Rule 2. First character of name of any function must be either alphabets or underscore.
Valid name: _calulate, _5,a_, __ etc.Invalid name: 5_, 10_function, 123 etc.
More example click here
Rule 3. Name of function cannot be any keyword of c program.
Invalid name: interrupt, float, asm, enum etc.More example click here
Rule 4. Name of function cannot be global
identifier.
Valid name: __TOTAL__, __NAME__ ,__TINY__etc.
Invalid name: __TIME__,__DATE__, __FILE__,__LINE__,__STDC__
More example click here
Note: It is good practice to not write the variable name in the above format.
Rule 5: Name of function cannot be register Pseudo variables
Register Pseudo variables are:
_AX _AL _AH _SI _ES
_BX _BL _BH _DI _SS
_CX _CL _CH _BP _CS
_DX _DL _DH _SP _DS
_FLAGS
For examples click here.Rule 6.
Name of function cannot be exactly same as of name of other function or identifier within the scope of the function.
Good example click here
Rule 7. Name of function is case sensitive.
Example
Rule 8. Only first 32 characters are significant of the function’s name.
Example:
abcdefghijklmnopqrstuvwxyz123456aaa,
abcdefghijklmnopqrstuvwxyz123456bbb,
abcdefghijklmnopqrstuvwxyz123456cad
All three function name are same because only first 32 characters has meaning. Rest has not any importance.
8 comments:
Thank you so much.
thanks a lot...
Thanxxxxxxxx...........
elango said
very useful for beginers
Thanks
I wish I had seen these tutorials during my first semester. This guy is so good. much better than my former professor. Thanks for this. I hope you are a professor in a University somewhere. God bless you more.
In the example.. while u return the sum of 3 and 4 and storing it in p, u should use p in printf statemen..
i.e., printf(“%d”,p);
instead of printf(“%d”,sum);
hullo, i need to write a programme that outputs an arithmetic calculator. please help.
Post a Comment