User defined function

Introduction


User defined function
User defined function

Functions can be classified into two categories, namely, library functions and user de­fined functions, main is an example of user-defined functions, printf ,getch,clrscr and scanf belong to the category of library functions. We have also used other library functions such as sqrt, cos, strcat, etc. The main distinction between these two categories is that library functions are not required to be written by us whereas a user-defined function has to be developed by the user at the time of writing a program.

In C, main is a specially recognized function. Every program must have a main function to indicate where the program has to begin its execution. While it is possible to code any program utilizing only main function, it leads to a number of problems. The program may become too large and complex and as a result the task of debugging, testing, and maintaining becomes difficult. If a program is divided into functional parts, then each part may be independently coded and later combined into a single unit. These subprograms are referred to as functions.
There are times when certain type of operations or calculations are repeated at many points throughout a program. For instance, we might use the factorial of a number at several points in the program. In such situations, we may repeat the program statements wherever they are needed. Another approach is to design a function that can be called and used when­ever required. This saves both time and space.

This “division” approach clearly results in a number of advantages.

  1. It facilitates top-down modular programming as shown in Fig. 9.1. In this program­ming style, the high level logic of the overall problem is solved first while the details of each lower-level function are addressed later.
  2. The length of a source program can be reduced by using functions at appropriate places. This factor is particularly critical with microcomputers where memory space is limited.
  3. It is easy to locate and isolate a faulty function for further investigations.
  4. A function may be used by many other programs. This means that a C programmer can build on what others have already done, instead of starting all over again from scratch.

Modular Programming

Modular programming is a strategy applied to the design and development of soft­ware systems. It is defined as organizing a large program into small, independent program segments called modules that are separately named and individually callable program units. These modules are carefully integrated to become a soft­ware system that satisfies the system requirements. It is basically a “divide-and-conquer” approach to problem solving.Modules are identified and designed such that they can be organized into a top-down hierarchical structure (similar to an organization chart). In C, each mod­ule refers to a function that is responsible for a single task.

Some characteristics of modular programming are:

  1. Each module should do only one thing.
  2. Communication between modules is allowed only by a calling module.
  3. A module can be called by one and only one higher module.
  4. No communication can take place directly between modules that do not have calling-called relationship.
  5. All modules are designed as single-entry, single-exit systems using control struc­tures.

Elements of user-defined functions

Functions are classified as one of the derived data types in C. We can therefore define functions and use them like any other variables in C programs. There exist some similarities between functions and variables in C.In order to make use of a user-defined function; we need to establish three elements that are related to functions.

  1. Function definition.
  2. Function call.
  3. Function declaration.

The function definition is an independent program module that is specially written to implement the requirements of the function. In order to use this function we need to invoke it at a required place in the program. This is known as the function call. The program (or a function) that calls the function is referred to as the calling program or calling function. The calling program should declare any function (like declaration of a variable) that is to be used later in the program. This is known as the function declaration or function prototype.

In next post,I will explain the methodologies of function definition.

This entry was posted in Uncategorized. Bookmark the permalink.