Functions can be classified into two categories, namely, library functions and user defined 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 whenever required. This saves both time and space.
This “division” approach clearly results in a number of advantages.
- It facilitates top-down modular programming as shown in Fig. 9.1. In this programming style, the high level logic of the overall problem is solved first while the details of each lower-level function are addressed later.
- 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.
- It is easy to locate and isolate a faulty function for further investigations.
- 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 is a strategy applied to the design and development of software 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 software 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 module refers to a function that is responsible for a single task.
Some characteristics of modular programming are:
- Each module should do only one thing.
- Communication between modules is allowed only by a calling module.
- A module can be called by one and only one higher module.
- No communication can take place directly between modules that do not have calling-called relationship.
- All modules are designed as single-entry, single-exit systems using control structures.
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.
- Function definition.
- Function call.
- 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.