Functions allow the programmer to break large tasks into smaller subsets of code. In other words a function is a sub-unit of a program which performs a specific task. In previous examples we have been using printf, which is itself a function. Functions take arguments (often passed through variables) and may return values. Functions are a basic building block of the C language, and are very handy when you have to repeat a specific task many times.

Note that functions may produce "side effects" if they modify variables or memory locations that can be accessed by other functions. Such side effects can make it difficult to determine what a program will do just by reading its code. In C, it is up to the programmer to limit the amount of side effects that happen. This can be done by making functions reflect, as much as possible, real mathematical functions, which only take inputs and return an output without causing any side effects.

Once you have written a function, it can be accessed from other functions (in addition to main). This allows us to build complex programs from simple functions.

Each function definition has the form:

Simple function example:

The function maximum takes two integer arguments as input, and returns the larger value of the two. maximum must be defined before it can be called, but in this code its full definition doesn't come until line 13. To resolve this, a "function prototype" for maximum is defined on Line 2. The prototype tells the compiler some critical information about the function before it is used, ie. what arguments it takes (if any) and what value it returns (if any). On line 7, the function is called and the return value is assigned to the variable k.

As mentioned above, functions can access other functions (a function can even call itself). Say you wanted to change the maximum function to determine the maximum of three integers instead of just two. One could rewrite the function as:

Alternatively, the programmer could reuse the existing maximum function as:

This technique is called "function nesting". The inner function call maximum(a,b) will return the largest value of a and b just as in the previous example. Next the maximum function will evaluate the maximum of the return value and variable c. Although a simple example, this also demonstrates how functions can help mitigate a problem know as code duplication. Code duplication occurs when the same code is "copied and pasted" in to more than one place. If a bug is found in this code segment, it can be difficult to go back and find where all instances or variations of the code exist individually and fix them, but if instead all of those duplicated blocks of code are replaced with a call to the same function, it is very easy to fix the definition of a single function.

It is worth noting that functions do not need an input value and do not need to return a value. Functions that do neither are called "void" functions. A programmer may be printing out a series of dashes as delimiters for output and could write a function as follows:

In this example, there is no input and no return value. You can call this function from other functions like so:

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement