what-is-function |
A piece of code within a larger program, which is written to
perform a specific task is called function. It is independent of the remaining
code of the program. Functions are the building blocks of C program.
A program may have many functions. Each functions has a
unique name. The function is called for executing with the reference of its
name. A function can be called for executing by the main () function or any
other function of the program.
Functions provide structured programming approach. In
structured programming, a large program is divided into smaller units. These
small units are called functions or modules, or subprograms.
If a program is written without using functions then there
may be repetition of the same piece of code at various places. So thje size of
the program may become very large it becomes very difficult to modify and debug
the program.
Using functions, a program can be divided into different
units. Each unit can be managed easily. A piece of code for each unit is
written as a function. The same piece of code is written only once in the
program as a function. This function can be called again and again from
multiple places in a program to perform a particular task. The size of program
is reduced. The program can easily be modified and debugged.
Benefits / Advantages
of using function
There are many advantages or benefits of using functions in
C. the main advantages are as follows:
Easy to write a
program
A problem becomes easier if it is broken down into smaller
tasks. Similarly, writing a large program becomes easier if it is divided into
smaller parts. Each part is called a function.
Easy to understand
and modify the program
When large program is divided into subprograms, the
complexity of their program is reduced. It makes easier to understand the logic
of the program. It is also easy to make changes and detect errors in the
program.
Eliminate duplicate
code
The same piece of code is written only once in the program
as a function. The function is called for executing with the reference to its
name only. Thus same piece of code does not have to written at multiple places
in the program to perform the same task. This reduces the size of the program.
Reusability
The function written for one program can be re-used into
another new program. Thus, the programmer can develop a program quickly in a
very short time. Suppose a function is written to draw a line graph. This
function can be used in any program that needs to draw a line graph.
·
Built-in functions
·
User-defined functions
Built-in Functions
The predefined functions that are part of the programming
language are called built-in functions. The built-in functions are also called
library functions. These functions can be used for different purpose. These
functions make the programmer’s job easier to design the program. The program
development time is also reduced by using built-in functions.
Every language has a large number of built-in functions.
These functions are declared in the header files for example the printf()
functions are declared in header file stdioi.h Similarly clrscr functions
declared in conio.h header file. Every language has a large number of header
files. Each header file has information of related function
User-Defined
Functions
The functions written by user or programmer are called
user-defined functions. These functions are also called programmer-defined
functions. A user-defined function is written to perform a specific task. A
program may contain many user-defined functions.
A user-defined functions has the following main parts:
Function declaration or prototype
Function definition
What is function?
Reviewed by JD Ahmad
on
June 11, 2018
Rating:
Subscribe to:
Post Comments
(
Atom
)
No comments: