ProgrammerGuide.Net | Step by Step Programmer Guide

C-User-Defined Function

User-Defined Function,

INTRODUCTION
We have mentioned earlier that one of the strengths of C language is that C function are easy to define and use. We have used functions in every program that every program that we have discussed so far. However, they have been primarily limited to the three functions, namely, main, printf, and scanf. In this chapter, we shall consider in detail how a function is designed, how two or more function are put together and how they communicate with one another.

The single worst feature of Old C was that there was no way to declare the number and types of a function’s arguments and to have the compiler check that the use of the function was consistent with its declaration. Although it didn’t do a lot of damage to the success of C, it did result in portability and maintainability problems that we all could have done without.

The Standard’s way of fixing this problem was in large measure to plagiarize from C++, which had already tried out the new ideas in practice. This model has been so successful that lots of ‘Old’ C compilers adopted it on their way to conforming to the Standard.

C functions can be classified into two categories, namely, library functions and user-defined functions. main is an example of user defined functions. printf 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 category in that library function are not to be 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

NEED FOR USER-DEFINED FUNCTION
As pointed out earlier, main is a specially recognized function in C.  Every program must have a main function to indicate where the program has to be begin its execution. While it is possible to code any program utilizing only main function, it leads to a number of problem. The program may become to large and complex and as a result the task of debugging, testing, and maintaining becomes difficult.

There are times when certain type of operations or calculations is 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.

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

1. It facilitates top-down  modular programming  as shown in figure 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.

2. It is easy to locate and isolate a faculty for further investigation.

3. 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.

A MULTI-FUNCTION PROGRAM
A function is a self-contained block of code that performs a particular task. Once a function has been designed and packed, it can be treated as a ‘block box’ that takes some data from the main program and a return value. The inner details of operation are invisible to the rest of the program. All that the designed using a collection of these black boxes known as functions
void printline (void)
{
    int i;
for (i=1; i<40; i++)
    printf(“_”);
printf(“\n”);
}
The above set of statements defines a  function called printline, which could print a line of 39-character length. This function can be used in a program as follow:

 ELEMENTS OF USER-DEFINED FUNCTIONS
We have discussed and used a variety of data types and variables in our programs so far. However, declaration and use of these variables were primarily done inside the main function. As we mentioned in chapter 4, functions are classified as one of the derived data type in C. We  can therefore define functions and use them like any other variable in C programs.

1. Both function names and variable names are considered identifiers and therefore they must adhere to the rules for identifiers.

2. Like variables, functions have (such as int ) associated with them.

In order to make user-defined function, we need to establish three elements  that are related to functions.

1. Function definition

2. Function call.

3. Function declaration.

Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?