Function In C

OVERVIEW

The idea of modular programming is the result of inspiration from the hardware manufacturing where replicable components of different items are available. If a component of an item gets out of order, it is replaced with a newer one. Many different components from different manufacturers can be combined together to form a hardware device such as computer, cars, and washing machines. Functions are the building blocks of C programs. They encapsulate pieces of code to perform specific operations. Functions allow us to accomplish the similar kinds of tasks over and over again without being forced to keep adding the same code into the program. function perform tasks that may need to be repeated many times.
                 In programs we have seen so far, the whole program logic was contained in a single main function. This style of writing programs is known as unstructured programming. Here I shall discuss structured programming approach. It is a modular way of writing programs. The whole program logic is divided into number of smaller modules or function. The main function calls these functions where they are need. A function is a self-contained piece of code with a specific purpose.

Importance of Functions 

A program may have repetition of a piece of code at various places. without the ability to package a block of code into a single function, programs would end up being much larger. But the real reason to have functions is to break up a program into easily manageable chunks. The use of functions provides several benefits. Some of them are:

  • They make programs significantly easier to understand and maintain. The main program can consist of a series of function calls rather than countless lines of code.
  • Functions increase re-usability of the code. Well written functions may be re-used in multiple programs. The C standard library is an example of the re-use of functions.
  • Different programmers working on one large project can divide the workload by writing different functions, hence ensuring the parallel development of the software.
  • Functions can be executed as many time as necessary from different places in the program.
  • When an error arises, rather than examining the whole program, the infected function is debugged only.

Types Of Functions

There are two types of functions in C:

    (ii)    Built-in functions
    (iii)   User-defined functions

Built-in Function

Built-in functions are predefined functions that provide us convenient ways to perform variety of tasks. These functions are packaged in libraries. Through these functions we can easily access complex programming functionality. We should not reinvent that wheel. All that we need to do is just making a function call and the rest of the task is performed by the called function.
            We are familiar with some of the built-in functions, e.g., we have seen c type library and some of the functions defined in it, we have also been using print f and scan f functions throughout the Function in C, which are defined in the library of console input/output.

To use a built-in function in C, I must have to include in wer program the header file containing its declaration. For example to user printf() and scanf(), we have to include stdio.h file in wer program.

Built-in functions are not sufficient for solving every kind of problem. A programmer may need to write his/her own functions depending on the nature of problem being solved. Such functions are called user-defined functions.

Writing Functions in C:

We are familiar with the main() function, which is the mandatory part of every C program. In page of getting started with C, I have introduced the structure of the main function. Every function in C has almost the same basic structure. A function in C consists of a function header which identifies the function followed by the body of the function between curly braces containing the executable code for the function. Every function in C is written according to the following general form:

returen_type FunctionName   (parameter_list)
{
         Executable  Statement (s)

         return expression;
}



















































































     

Comments

Popular posts from this blog

Data Base Design Process

Data Base

Data Integrity And Normalization