Last updated on June 13th, 2020 at 08:26 pm
User defined functions
Functions can be classified into two categories, namely, library functions and user-defined functions. The functions which are developed by user at the time of writing a program are called user defined functions. Thus, user defined functions are functions developed by user.
Example:
Here addNumbers() is an user defined function.
//declaring an user defined function which has been defined later
float addNumbers(float a, float b);
int main(){
float result;
/* calling user defined function from the main function */
result = addNumbers(.5, .8);
return 0;
}
//defining an user defined function which has been declared earlier.
float addNumbers(float a, float b){
return (a+b);
}
Read More: What are the elements of user defined functions?
Advantages of User Defined Functions
When not using user defined functions, for a large program the tasks of debugging, compiling etc may become difficult in general. That’s why user defined functions are extremely necessary for complex programs. The necessities or advantages are as follows,
- It facilitates top-down modular programming. In this programming style, the high level logic of the overall problem is solved first while the details of each lower-level function are addressed later.
- The length of a source program can be reduced by using functions at appropriate places.
- It is easy to locate and isolate a faulty function for further investigations.
- A function may be used by many other programs. This means that a programmer can build on what others have already done, instead of starting all over again from scratch.