Chapter 6 *Functions

Table of Contents
6.1 Definition of a Function
6.2 Function Declaration and Function Prototypes
Written by Danny Rodriguez. Initial markup by Dave Chapeskie.

6.1 Definition of a Function

The goal of this chapter is to expand upon the details of a function as briefly introduced in Chapter 2. You'll find functions to be a vital concept of C, as having at least one function is mandatory for all C programs.

A function, by definition, is a procedure or routine. In other words, it's a section of code that executes a certain task. Although some languages do distinguish between procedure and function, whereas a function returns a value of some kind and a procedure does not, C combines both functionalities into its definition.

Note: Definitions adhere to ANSI C.

Example 6-1. Function definition

    (1)return_type (2)function_name ((3)datatype parameter_name, parameter_name, (4)...)
    {
        /* ... code ... */
    }

An example C function definition is given in Example 6-1, a description of it's parts follows.

(1)
This datatype represents the type of data you want to return upon completion of the function. Any valid datatype can be used.
(2)
This is the identifier name of the function. It follows the same variable naming convention as described in Section 2.2
(3)
This section defines the arguments that must be specified when the function is called.
(4)
The ``...'' at the end of the parameter list indicates a variable argument list. That is, the function can accept an unspecified number of additional arguments. The scanf and printf family of functions are the most common examples of these kinds of functions.

Example 6-2. A simple adding program with functions

    /*
       A simple calculator program with functions.
    
       This program allows the user to input two numbers, then passes those
       two numbers into a sum function, and finally prints the returned sum.
    */
    
    #include <stdio.h>
    
    /* Define a function that takes two arguments, both of type int, and
       returns an int */
    int(1)
    sum (int n1, int n2)
    {
        int answer;
        answer = n1 + n2;   /* Put the sum of the two numbers into answer */
        return answer;(2)       /* Exit function and return answer */
    }
    
    
    int
    main (void)
    {
        int number1;
        int number2;
        int total;
    
        printf ("What is the first number? ");
        scanf ("%d", &number1);
    
        printf ("What is the second number? ");
        scanf ("%d", &number2);
    
        total = sum (number1, number2);(3)   /* Get the sum of the two numbers from the
                       return value of sum() */
    
        printf ("The sum of %d plus %d is %d\n", number1, number2, total);
    
        return 0;
    }

Consider the simple calculator program presented in Example 3-1 from Chapter 3. Within the main function we prompted the user for two numbers, and then printed their sum. Well, now let's create a function to take two numbers, and return their sum. This is shown in Example 6-2.

(1)
This is the start of the definition of the function sum.[1] As you can see, this function accepts two integer values, n1 and n2, and returns an integer result.
(2)
Here the sum is returned from the function using the return statement.
(3)
Here the main function calls the sum function passing the two numbers as arguments and then storing the return value into total.

We defined sum as a user defined function. That is, it is a custom function that we've created. Functions such as scanf and printf, used in Example 6-2, are called library functions. They exist in a pre-defined library of functions that at our disposal.

Keep in mind, this is a very basic example. Using functions avoids redundant code and provides better readability throughout a program. Imagine if instead of a simple summation if the function had to a much more complicated calculation. The calculation would be nicely separated from the input and output code in main.

There are a few noteworthy things to remember about functions:



Notes

[1]

As first mentioned in Section 2.1 functions are usually referred to in comments or other text, like this book, by their name with parentheses. These parentheses are used just to indicate that the name refers to a function and are left empty; the arguments are not listed.

This, and other documents, can be downloaded from http://opencbp.sourceforge.net/.

For questions about the Open C Book Project, read the documentation.