Typically, a C program consists of many small functions, which reside in many different files.
The standard C library provides many, many useful functions.
Look some of them up using the man page in UNIX. (Try putc, getc, atoi, strlen for starters.) Take a look around.
Make certain you understand the filecopy.c program we worked on in class. Go through the program statement by statement.
Every function has the form:
returnValue fcnName(argumentList)
{
function body
}
Notice how this format matches the format of the function defined in the last section of this document.
Below is an example showing how to call this function.
long factorial(int n);
main()
{
long fact;
fact = factorial(10);
printf("10 factorial is %d\n",fact);
}
This code can be abbreviated by calling the factorial function from within the printf statement. This is an example of how a function evaluates to its return type and can therefore be used in place of a data value or expression.
long factorial(int n);
main()
{
printf("10 factorial is %d\n",factorial(10));
}
To read more on functions, their arguments and return values please check a standard C documentation guide.
Exercise
Code a function to find the minimum of three integers. Accept these three integers as arguments to the function. Return the lowest integer. Call this function from main. Print out the result.