Coupon Accepted Successfully!



In our daily life, most of the complex works are done by a group of people. This results in better productivity and quality. Work is done at a great speed. Normally, big work is distributed among many people. For example, to declare the result of examination in an autonomous institute, marks/score of all subjects, practical, seminar, projects, etc., are collected from different departments through professors and the result is compiled, declared, and displayed.

The same concept is applied while developing complex programs/software applications using programming languages. In case the size of the program is large, it becomes difficult to maintain it and it is also hard to identify the flow of data. It is preferred to divide the large program into small modules called functions. Each function takes the data that are provided by the main() function and carries out operations as per the requirement, and results can be written to the calling function. Thus, programs are made more efficient.

Program without functions

When the concept of function or sub-program was not introduced, the program sizes were large and few codes get repeated as shown in the Figure. It was very difficult to debug and update these large programs because many bugs get encountered. When the functions and sub-programs are introduced, the programs are divided into a number of segments and code duplication is avoided as shown in Figure. Bugs in small programs can be searched easily and this step leads to the development of complex programs with functions.

Program with function

One of the features of C/C++ language is that a large program can be divided into smaller sub-programs. The smaller programs can be written in the form of functions. The process of dividing a large program into tiny and handy sub-programs and manipulating them independently is known as modular programming. This technique is similar to divide-and-conquer technique. Dividing a large program into smaller functions provides advantages to the programmer. Testing and debugging a program with functions is easier. A function can be called repeatedly based on the application and thus the size of the program can be reduced. The message passing between a caller (calling function) and callee (called function) takes place using arguments. The concept of modular approach of C++ is obtained from a function. Advantages of the functions are as follows:

  • Reusability: A function once written can be invoked again and again, thus helping us to reuse the code and removing data redundancy.
  • Modularity: Functions can help us in breaking a large, hard to manage problem into smaller manageable sub-problems. It is easier to understand the logic of sub-programs.
  • Reduced Program Size: Functions can reduce the size of the program by removing data redundancy.
  • Easy Debugging: Using functions, debugging of a program becomes very easy, as it is easier to locate and rectify the bug in the program if functions are used.
  • Easy Updating: If we need to update some code in the program, then it is much more easier in case we have used functions, as the changes need to be made in one place only (in function).

The programs written in ‘C++’ language are highly depending on functions. The ‘C++’ program is nothing but a combination of one or more functions. Execution of every ‘C++’ program starts with user defined function main(). The method of using functions is slightly changed and enhanced in C++ as compared to C. The C++ language adds few new features to function like overloading of functions, default arguments etc. These features are described in ahead.

C++ functions are classified in two categories. They are (1) library functions and (2) user-defined functions. The library functions can be used in any program by including respective header files. The header files must be included using #include pre-processor directive. For example, a mathematical function uses math.h header file.

The programmer can also define and use his/her own functions for performing some specific tasks. Such functions are called user-defined functions. Every C++ program consists of main() function. This is a user-defined function in which program statements are written by the programmer according to the problem definition.

Test Your Skills Now!
Take a Quiz now
Reviewer Name