Programming in C UNIT 7
Programming in C UNIT 7
Introduction:
Function is a block of code or statement to perform some specific (particular) task. Every c
program has at least one function, which is main() and all other trivial program can define
additional function.
We can divide up our code into separate function, but logically the division in such way that
each function performs a specific task.
A single function can be accessed or called from different places with in a program. After
execution of the intended task, the control will be returned to the point from which the
function was accessed or called.
int main()
{ Int main()
{
abc ();
2) User-defined function:-
A user defined function is declared, defined, and used
by the programmer according to their need.
Difference
Library function between Library and user defined function
User-defined
function:
1) A user-defined function is not a
1) The library function is predefined function predefined function, it is defined by the
in a header file pre-processor directive programmer according to the need.
2) The programmer can simply use this 2) The programmer has to declare define
function by including the respective header and use this function by them itself.
file.
3) Program development time will be faster. 3) Program development time will be
usually slower
4) The program using the library function will 4) The program using a user-defined
be usually simple function will be usually complex.
5) This function requires a header file to use 5) This function requires a function
it. prototype to use it.
6) Example:- printf(), scanf(), getch(), strlen() 6) Example:- Sum(), area(),factorial() etc.
etc.
3) Function definition:-
The collection of program statement that describes the specific task to be done by the
function is called function definition.
Function definition consists of function header which defines function name returns type
and its arguments and consist of function body which in enclosed in curly braces.
Syntax:-
returntype functionname(datatype variablel, datatype variable2,.......)
{