User Defined Functions – Programming Language C

Introduction to User Defined Functions

We have mentioned earlier that one of the strengths of C language is C User Defined Functions. They are easy to and use. We have used functions in every program that we have discussed so far. However, they define have been primarily limited the three functions, namely, main, printf, and scanf. In this chapter, we to shall consider in detail the following,

  1. How a function is designed?
  2. How a function is integrated into a program?
  3. How two or more functions are put together? and
  4. How they communicate with one another?

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 sart, 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.

However, a user-defined function can later become part of the C program library. In fact, this is one of the strengths of C language.

NEED FOR USER-DEFINED FUNCTIONS

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 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 independently coded programs are called subprograms that are much easier to understand, debug, and test. In C, such 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.

void printline(void);/* declaration */
  main()
  {
      printline();
      printf("This illustrates the use of C functions/n");
      printline();
   }
     void printline(void)
   }
       int i;
       for(i=l i<40; i++)
       printf("-");
       printf("\n");
   }

This program will print the following output: This illustrates the use of C functions

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

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

A MULTI-FUNCTION PROGRAM

A function is a code particular task. designed and packed, it can be treated as a ‘black box’ that takes some data from the main program and returns a value. The inner details of operation are invisible to the rest of the program.

What goes in and what comes out. Every C program can be All that the program knows about a function is: designed using a collection of these black boxes known as functions.

ELEMENTS OF USER-DEFINED FUNCTIONS

We have discussed and used a variety of data types and variables in our programs so far. However these variables were primarily done inside the main function. As we declaration and use of mentioned in next post. 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. It is therefore not a surprise to note that there exist some similarities between functions and variables in C.

  • Both function names and variable names are considered identifiers and therefore they must adhere to the rules for identifiers.
  • Like variables, functions have types (such as int) associated with them.
  • Like variables, function names and their types must be declared and defined before they are used in a program.

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

Function Call

A function call is a postfix expression. The operator (..) is at a very high level of precedence. Therefore, when a function call is used as a part of an expression, it will be evaluated first, unless parentheses are used to change the order of precedence.

In a function call, the function name is the operand and the parentheses set which contains the actual parameters is the operator. The actual parameters must match the function’s formal parameters in type, order and number. Multiple actual parameters must be separated by commas.

Read More Topics
Interactions between ipv4 and datalink layer
TCP connection establishment in computer networks
The User Datagram Protocol (UDP)
Transport layer in reference model

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply