Function is block of program. When any program is very long then we try to cut the program in different parts( or blocks) so that whole program is more understandable and easier to debug (error checking).This part of program is called function. It is also useful when set of program uses many times in the program then instead of writing whole program in many time we only call the function.
There are two type of function.
(1) Library function (Predefined function)
(2) User defined function
Syntax:
Return_type Function_name ( Parmeter1,parametre2,parameter3,…..)
{
Statement 1;
Statement 2;
……………
……………
……………
return <>
}
Fuction_name: It can be any valid identifier like India,sum,_world etc.
Return_type: In can be any valid data type like int,char,float ,void etc (default int)
Return type is required when you want function should return any value and if you don’t want that function return any value then write void as a return type.
Parameter list: It is set of value which you want to give the function and they are separated by comma. (default void)
Simple example :
int sum (int,int) //function declaration
void main()
{
int p;
p=sum(3,4); //function call
printf(“%d”,sum);
}
int sum( int a,int b) //function definition
{
int s; //function body
s=a+b;
return s; //function returning value
}
Output 7
Since definition of sum function is written by some user. So it is called user defined function. This function can calculate the sum of any two integers. You are also using printf() function but you are not writing definition of printf() function because printf is predefined function. Your TURBO C know about printf function but he doesn’t know about your sum () function;
1 comment:
Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!
Post a Comment