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. |
||||||
| 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. |
||||||
| Parts of User Define Function |
There are three
parts of User Define Function: |
| 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. Syntax
ReturnType FunctionName (ArgumentList); Here, ReturnType 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. Example 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. |
| 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. Syntax
ReturnType FunctionName (ArgumentList) { Function Body (Group of Statements) } Function Body
contains: Example
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. |
| 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. Syntax
FunctionName (Arg1,Arg2,...ArgN); When function
is called, compiler will compare function call with function declration to check: Example
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. |