What is Functions explain with example.
C provides functions which are again similar most languages. One difference is that C regards main() as function. Also unlike some languages, such as Pascal, C does not have procedures -- it uses functions to service both requirements.
Let us remind ourselves of the form of a function:
returntype fn_name(1, parameterdef2, )
{
localvariables
functioncode
}
Let us look at an example to find the average of two integers:
float findaverage(float a, float b)
{ float average;
> average=(a+b)/2;
return(average);
}
We would call the function as follows:
main()
{ float a=5,b=15,result;
result=findaverage(a,b);
printf("average=%f n",result);
}
Note: The return statement passes the result back to the main program.
Labels: C