Function definition in c programming



Definition


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.

Syntax of function in c programming






Example of simple function


#include<stdio.h>
int sum (int,int);    //function declaration

int main(){
int p;
p=sum(3,4);       //function call
printf(“%d”,sum);
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.

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.

Rule 3:  Name of function cannot be any keyword of c program.
Invalid name: interrupt, float, asm, enum etc.
To know all keyword of C click here

Rule 4: Name of function cannot be global identifier.

Valid name: __TOTAL__, __NAME__  , __TINY__etc.
Invalid name: __TIME__ ,__DATE__ , __FILE__ ,__LINE__ ,__STDC__

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:

Rule 6: Name of function cannot be exactly same as of name of other function or identifier within the scope of the function.

Example:

(q) What will be output of following program?

#include<stdio.h>
float lata();

int main(){
    float lata;
    lata=lata();
    printf("%f",lata);
return 0;        
}

float lata(){
    return 4.5f;
}

Output: Compiler error

Rule 7: Name of function is case sensitive.

Rule 8: Only first 32 characters are significant of   the function’s name.
Examples:

abcdefghijklmnopqrstuvwxyz123456aaa, 
abcdefghijklmnopqrstuvwxyz123456bbb, 
abcdefghijklmnopqrstuvwxyz123456cad

All three function name are same because only first 32 characters has meaning. Rest has not any importance.


2 comments:

Anonymous said...

nice blog.... if you want to start learning 'c'... u can also visit a website disemble.blogspot.com

Anonymous said...

explain the meaning of these : C programming

(i)int f(int a)
(ii)double f (double a,int b)
(iii)void f(long a,short b, unsigned c)
(iv)char f(void)
(v)unsigned f(unsigned a , unsigned b)