User Define Function

What is User Define Function?

User Define Function is a group of logically related statements that is used to perform specific task.
User Define Functions are defined by user as per their requirenments.
main () is an example of User Define Function.

Advantage and Disadvantages of User Define Function

(1) In a large and complex programs certain task need to be performed repeatedly for different input values. If we define User Define Function for that task then there is no need to write same block of code again and again. So it will reduce length of the program.
(2) Hence there is no repeatation of same block of code again and again in the program it is easy for the user to understand the program.
(3) If something went wrong with the code in the function it is easy to identify and correct the error.
Disadvantage of User Define Function is that it will takes lots of extra time for program execution.

Parts of User Define Function

There are three parts of User Define Function:
(1) Function Declaration
(2) Function Definition
(3) Function call

Function Declaration

As it is compulsory to declare all the variables in C Program it is also compulsory to declare User Deifne Function in C program. Declaration of Function is also known as Prototype of Function.
Function can be declared either inside main() function or outside all the function at the top (after header files).

ReturnType FunctionName (ArgumentList);
indicates type of the value returned by the function.
FunctionName indicates name of the function.
ArgumentList is a collection of DataType Variable pair followed by comma.
int sum(int a,int b);

The variables that are passed as an argument to the function are known as formal parameter or dummy variables.
Declaration of the Function is useful for the compiler. It provides following information to the compiler:
(1) Name of the Function
(2) ReturnType of the Function
(3) Number of Arguments to be passed to the Function
(4) Datatype of the arguments.
Whenever any user define function is called compiler first check function declration for above mentioned information.
Points to be remember while declaring function:
(1) If function does not return any value then specify void as a return type.
(2) If function does not accepts any argument then specify void in the argument list or leave the parantheses blank.
(3) Each argument must be declared seperately in argument list.
(4) It is not compulsary to specify name of the variable in argument list. 

Function Definition

Function Definition contains actual logic or code to be executed. Function Definition is a collection of logically related statements that is used to perform specific task.

ReturnType FunctionName (ArgumentList)
   Function Body (Group of Statements)

Function Body contains:
   (1) Local Variable Declaration
   (2) Executable Statements
   (3) Return Statement (Optional)

int sum (int a, int b)
   int c;
   c = a + b;
   return c;

If return type of the function is void then there is no need to write return statement. 
all the Function can be defined outside main() function at bottom.

Function Call

In order to perform specific task that is defined inside function definition, the control of the program need to be transfered from main () function  to function definition.
Program control can be transfered from main () function to appropriate function definition by calling the function.

FunctionName (Arg1,Arg2,...ArgN);

When function is called, compiler will compare function call with function declration to check:
(1) Name of the function
(2) Number of arguments passed to the function
(3) Data type of passed arguments
(4) Returntype of function
If function call does not match with function declration then compiler will generate compile time error.

int sum (int a,int b);// Function Declaration
sum(4,5,6); // Function call
above function call will generate error because number of arguments not match.

Download Projects

Download Programs