Functions in C++ allow programmers to encapsulate pieces of code to perform specific operations. Functions divide a program into different parts that are easier to code, modify, maintain, and reuse. Using functions makes programming easier and less time-consuming as code can be divided among programmers working in parallel.
2. Functions:
A function is a named block of code that
performs some action. The statement
written in a function is executed when it is
called by its named. Each function has a
unique name. Function is the building
blocks of C++ programs. They
encapsulate pieces of code to performed
specified operation.
3. Importance of function:
A program may need to repeat the same
piece of code at various places. It may be
required to perform certain tasks
repeatedly. The program may become
very large if functions are not used. The
piece of code that is executed repeatedly
is stored in a separate function. The real
reason of using functions is to divide a
program into different parts. These parts of
a program can be managed easily.
4. Advantages of function:
Easier to Code: A lengthy program can be
divided into small function. It is easier to
write small function instead of writing one
long program. A function is written to solve
a particular program. A programmer can
focus attention on a specific problem. It
makes programming easier.
5. Easier to Modify:
Each function has a unique name and is
written as an independent block. If there is
any error in the program the change is
made to particular function in which error
exists. A small function is easier to modify
than a large program.
6. Easier to maintain:
Function is easier to maintain than long
programs. Each function contains
independent code. A change in the
function does not affect other parts of
program.
7. Reusability:
The code written in functions can be reused
as and when required. A function but can
be executed many times. A function is
written to solve a particular problem.
Whenever that problem has to be solved
the function can be executed.
8. Less Programming
Time:
A program may consist of many functions.
These functions are written as
independent programs. Different
programmers can work on different
functions at the same time it takes far less
time to complete the program.