5.1. Basics of Using Functions

  • Three required parts of code which make up the use of a function in a program are:
    1. The function prototype.
    2. The calling of the function.
    3. The definition of the function.
  • Variables, called arguments or parameters, may be passed to a function by value (not by reference). Functions may also access global variables. We will discuss this in detail when we talk about the rules of scope and storage classes.
  • A function may return a single value; however, it can also alter values in memory, which has the affect of returning additional data to the calling program (e.g., scanf()).

5.1.1. Function Prototypes

The function prototype defines:

  • The name of the function.

  • The type of value returned by the function.

  • The number of arguments passed to the function and the data type of the arguments passed to the function. Use three dots (…) to indicate variable number of arguments.

  • If the function does not return a value or if no arguments are passed to the function, the void keyword should be used.

    void myfunction1(void); /* no return or arguments       */
    int myfunction2(void);  /* returns an int, no arguments */
    void myfunction3(int);  /* no return, one int argument  */
    
  • Function prototypes are often placed in included files. (file.h)

  • Note: If the function definition is placed before main() and any other functions that call the function, then a function prototype is not needed. Although, in C it is a more common coding practice to use a function prototype and put main() as the first function in the file.

The following prototype declares a function that returns an integer and takes two integer arguments.

int myfunction( int, int );

5.1.2. Function Definition

The function definition defines the code used to evaluate the function.

int myfunction( int x, int y )
{
   double z;
   int a;
   .
   .
   .
   return(a);
}

5.1.3. K&R Style Functions

The syntax we use for the function prototypes and function definitions was specified by the ANSI standard for the C language. The original C language as defined in the classic book The C programming Language by Brian Kernighan and Dennis Ritchie, i.e, K&R C, uses a slightly different syntax. Most modern compilers complain about, but still compile programs written with this syntax. We show it here only so that when you read an old C program written with this syntax, then you will be familiar with the syntax.

int myfunction( );     /* the prototype */

int myfunction( x, y ) /* the definition */
int x;
int y;
{
   double z;
   int a;
   .
   .
   .
   return(a);
}

5.1.4. Return statement

  • The keyword return causes the function to exit, passing control back to the calling function.
  • A return is not required if the function is of type void.
  • return is the facility for returning a single value to the calling function.
  • The value being returned is not required to be inside parenthesis, but if the return contains an expression as opposed to a constant or individual variable, then it is common to put parenthesis around the expression.