Introduction To Functions
Functions are building blocks of C. The general form of C (User-defined) function is:Return-type-function-name(parameter list)
parameter declaration
{
body of function;
}
C functions can be divided into 2 types:
1.Library Functions:
Ex: printf() , scanf() ,etc.,
2.User-defined functions:
Ex: main(), fact(), etc.,
For the library functions,user needs to write the code whereas a user-defined function has to be developed by the user at the time of writing a program. If a program is divided into functional parts,t hen each part may be independently coded and later combined into a single unit.These sub-programs are called as "functions" are much easier to understand,debug and test.
Purpose Of Functions:
1) Using functions we can avoids rewriting the same code
over and over.
2) It is simpler to write a function that does a specific
job.
3) Programs with functions are compact.
4) It facilitates top-down modular programming.
5) Debugging the code is much simpler because errors can
be localized and corrected.
6) The length of the source program can be reduced.
7) It increases program readability and helps documentation.
8) Many other programs may use a same function.
Characteristics Of a Function:
1) A function can return only one value.
2) Return statement indicates exit from the function and
return to the point from where the function was invoked.
3) When a function is not returning any value, void type
can be used as a return type.
4) Parameter argument list is optional.
5) A call to the function must end with a semicolon.
6) A function can call any number of times.
7) A C function cannot be defined in other function.
8) C allows recursion i.e., a function can call itself.
Actual and Formal Parameters(arguments):
main()
{
statements;
calling function<--function1(a1,a2,.........am);-->Actual
parameters
statements;
}
called function<--function1(f1,f2,..........fm)-->Formal
parameters
statements;
{
statements;
}
Actual parameters or arguments are the parameters that are passed to the function whereas formal parameters or dummy parameters are the name of the corresponding parameters in the called function.
No comments:
Post a Comment