UNIX C Programming

Chapter 9: Functions

 

Objectives:

This chapter provides a more extensive look at functions and how to use them. In addition, it considers the concept of recursion and provides an introduction to pointers. The objectives important to this chapter include:

  • Use functions which deal with arguments and return values
  • Explain the underlying reasons for the use of functions
  • Explain function types
  • Compare ANSI-C prototyping to pre-ANSI-C forms
  • Discuss the concept of recursion and recursive calls
  • Clarify how to compile more than one program
  • Introduce students to the concept of a pointer
  • Discuss the address of (&) and indirection (*) operators
  • Demonstrate passing of values by address and by value
Concepts:

A function is a self-contained unit of program code designed to accomplish a particular task.

Functions save programming time, make a program easier to read and maintain, and make the program more modular. Functions enable the C programmer to think of a function as a "black box."

Review the difference between declaring a function, writing a function call, and defining a function. This has been covered in your text already. A prototype specifies the name of the function and the types involved. A call specifies the name of the function and the actual values passed. A definition specifies the name of the function, the types involved, and the variables that receive the values passed to the function.

ANSI-C encourages the form

	return_type function_name (argument declaration list);

when defining a function. The argument declaration list contains formal arguments. A function is called by a calling statement which contains actual arguments. It is like telling a function to expect to be handed two soft drinks (or ints or floats) when you declare it (prototype it or define it), and handing it a Diet Coke and a Mountain Dew when you call it.

Functions should be declared by type. A function with a return type should be declared the same type as the return value.

C permits a function to call itself. This is known as recursion. With recursion, review the following points:

  1. Each level of function call has its own variables.
  2. Each function call is balanced with a return.
  3. Statements in a recursive function that come before the recursive call are executed in the same order that the functions are called.
  4. Statements in a recursive function that come after the recursive call are executed in an opposite order from which the functions are called.
  5. Although each level of recursion has its own set of variables, the code itself is not duplicated.
  6. When the recursive call is placed at the end of a function, just before the return statement it is called tail or end recursion.

All C functions are created equal. Each can call any other function or by called by any other function.

While programs containing functions are usually placed in the same file, more than two files of functions can be compiled to produce an executable file.

Header files are often used to store #define directives used by functions in different files. Use the notation

	#include "file_name.h"

to include programmer-written .h file in a C program.

Students usually have difficulty with the concept of pointers. A pointer is a variable used to store an address. To assign an address of a variable called pooh to a pointer variable called ptr, use the format

	ptr type = &pooh;

To find the value stored in an address using the indirection operator and the format

	val = *ptr;

Thus,

	ptr = &pooh;
	val = *ptr;

means the same as

	val = pooh;

Pointers are declared using the format

	int * ptr;

where ptr points to type int. Yes, kids, this is yet another use of the asterisk. Whee!

Pointers can be used to pass the address of variables to functions. The function call:

	interchange( &x, &y);

passes the addresses of two variables to a function called interchange. That function might be defined as

	void interchange(int * u, int * v)

where u and v both point to type int. Note that the variables in the called function are different from the ones in the calling function. Since the variables are local to each function, the two sets could even have had the same names, and they would still be different variables. (Yes, you can take an aspirin now. I know I did.)