Functions are easy to use; they allow complicated programs to be parcelled up into small blocks, each of which is easier to write, read, and maintain.
A function is declared in the following manner:
return-type function-name
(parameter-list,…)
{body…}
return-type -is the variable type that the function returns. This can not be an array type . If not given, then int is assumed.
function-name -is the name of the function.
parameter-list -is the list of parameters that the function takes separated by commas. If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. If no variable type is in front of a variable in the paramater list, then int is assumed. Arrays and functions are not passed to functions, but are automatically converted to pointers. If the list is terminated with an ellipsis (,…), then there is no set number of parameters. Note: the header stdarg.h can be used to access arguments when using an ellipsis.
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);
}
