User defined Function
USER-DEFINED FUNCTIONS
What is function?
When it is used?
· When the program become very large and
complex and as a result the task of debugging, testing and maintaining become difficult.
· Than a program is divided in to small parts then each part may be independently code and letter combine in
to a single unit, this sub program called function.
· Function is much easier to understand,
debug and test the program.
· When certain types of operation or
calculation use repeated at many points through of program so this save both
time and space.
· A function is a self contain block of
code that perform a particular task.
Syntax:
return-type functionrf-name
(argument-list)
{
//function
body
}
Example:
void print _line(void):
main ():
{
print
_line():
printf("stop:):
}
void print _line (void)
{
for
int, i = o:i<g:i++)
Printf
("this is a computer"):
}
· The program execution begins with the
main function.
· During execution of the main the
first statement in counter is, print_ line():
1. At this point the program control is
transfer to the function print _ line.
2. After execute the print _ line
function , the control is transfer back to the main
3. Any function can call any other
function.
4. Function can call itself.
5. A function can be called more then
once.
6. A call function can be place either
before or after the calling the function..
Advantages of function
· The length of a source program can be
reducing by using function at appropriate place.
· It is easy to locate and isolate a
faulty function for further investigation.
· A function may be use by many other
programs.
· Function gives facilities top down
modular programming.
Elements of function
Three elements of function are:
1.
Function
definition :
2.
Function call
3.
Function
declaration
· A function definition is also known
as function implementation.
· The program that call the function is
refer to as the calling function.
· Function declaration is also known as
function prototyping.
What is function prototyping
· A function declaration is known as
the function prototyping.
· Function proto type concise of four
parts.
a) Function type (return type)
b) Function name
c) Parameter use
d) Terminating semicolon.
·
Syntax:
Function_type function_name(parameter list);
·
Example:
int mul
(int m, int n)
·
A prototype
declaration may be place in two places in a program
1.
Above all the
function
2.
Inside function
definition
·
When we place the
declaration above all the function the prototype is called global prototype.
·
When we place it
in a function definition the prototype is called a local prototype.
Difference between actual arguments and
formal arguments.
Actual
arguments
|
formal arguments
|
actual arguments are the source of information;
|
formal arguments are not the source of information;
|
The calling programs pass actual
arguments to called
functions.
|
The called functions access the information using
corresponding formal arguments.
|
actual arguments belong to the
calling function.
|
Formal arguments belong to the
called function.
|
A change in actual arguments would
be reflected in the formal arguments.
|
A change in formal arguments would
not be reflected in the actual arguments.
|
Actual arguments are not a copy of
the formal arguments.
|
Formal arguments are a copy of the
actual arguments.
|
Difference between function prototype and
function definition.
Function prototype
|
Function definition
|
The declaration, called the
function prototype, tells the computer the name, return type and parameters
of the function.
|
The function definition tells
the compiler what task the function will be perfoming
|
It has terminating
semicolon
|
It has no terminating
semicolon
|
The function prototype
consists of the function
|
The function definition
consists of the function.
|
Syntax
Return type
functionname(arguments)
|
Syntax
Returntype
functionname(arguments)
|
Difference
between call by value and call by deference.
Call by
value
|
Call by
reference.
|
In
call by value mechanism we have to pass the variable as the argument to the
function.
|
In
call by reference mechanism we have to pass the reference of the variable as
the argument to the function.
|
Syntax:
return-type
function-name (data-type variable-name)
{ . . . . . //function body}
|
Syntax:
Return-type
function-name (data-type & variable-name)
{ . . . .
. //function body}
|
In
call by value, if the value of the variable is change then it does not
reflect to the original value of the variable.
|
In
call by reference, if the value of the variable is change then it does
reflect to the original value of the variable.
|
It
creates the new set of variable for the function.
|
It
does not create the new set of variable but the reference of the variable for
the function.
|
Program
occupies more memory if the call by value mechanism is used.
|
Program
occupies less memory then the program uses the call by mechanism if the call
by reference is used.
|