• With this post we are moving on to the advance topics of C language.
  • In this post, I am going to discuss one of the most important concept of modern programming languages i.e Function. So let’s dive in.

A function is a block of code that performs a specific task.

  • When a set of related executable statements are places within a group then it is referred to as function.
  • We can use functions for the operations which are need to be repeated.

C functions can be classified into two categories:

  1. Library Functions
  2. User-defined Functions (UDF)

Library Functions

  • Library functions are provided by the C programming language.
  • They are also knows as in-built or ready-made functions.
  • User need not to define them. User can directly use them in the program.
  • Some examples of library functions are : printf(), scanf(), strlen(), gets(), clrscr(), etc.

 User-defined Functions (UDF)

  • User-defined functions are those functions which are written by the user (programmer).
  • Similar to variables, they need to be defined before using them because compiler dos not have knowledge about them.
  • main() is an example of user-defined functions.

Advantages of Functions ( Need for UDF)

  1. Re-usability

  • One of the major advantages of functions is re-usability.
  • There are situations in which we need to perform some operations or calculations multiple times. If we do not use functions, we have to rewrite that code again and again.
  • In such cases, function helps a lot. We can reuse the code which is already written. There is no need to rewrite that code again and again.
  • Function works on the principle “write once, use multiple times”
  • So this way, we have to write the functions once and it can be used for multiple times.
  1. Modularity

  • Function provides modularity into program by dividing large programs into smaller parts called modules.
  • These modules can easily be coded, tested and debugged separately.
  • Development will be fast since modules can be assigned to multiple programmers.
  • Each programmer is responsible to code, test and debug modules assigned to him or her only.
  • Programmers can work on the different modules of the same projects simultaneously.
  1. Easy debugging

  • Programs divided into functions can be easily debugged.
  • Since programs are sub-divided into modules, individual modules are coded, tested and debugged separately.
  • If any error is found or modules do not work properly, we only need to check the modules responsible for that task.
  • We need not to check the whole program starting from the scratch.
  • Since each module is small and implements limited functionality, they are easy to code, debug and modify.
  • It saves both time and labor.
  1. Easy management

  • Project management becomes easy as different modules are assigned to different team of programmers.
  • Programmers can work on separate modules simultaneously on the same projects.
  • Since task is divided into smaller modules, it becomes easy to handle and manage.
  1. Provides Readability

  • Programs are divided into different smaller modules and these modules further can be sub-divided for better understanding.
  • Programs written through functions allow better readability.
  • They are easy to read and understand.
  • It helps to understand program flow and logic.

That’s it for this post. Elements of Function will be discussed in the next post so please stay tuned!