2. • Definition - function
•
●A set of statements working together with
• common goal is known as function.
•
Also known as subprograms which are used to
●• compute a value or perform a specific task.
•
They can’t run independently and are always
●• called by the main() program or by some other
• function.
2
3. Functions
Function:
The strength of C language is to define and use function.
The strength of C language is that C function are easy to define and
use.
Function
Library User define
function function
4. Library function:
The library function are not required to be written
by us.
Eg:
printf
scanf
5. User define function:
User defined functions are self-contained blocks
of statements which are written by the user to
compute or perform a task.
●They can be called by the main program
repeatedly as per the requirement.
Eg: main()
‘main’ is specially used function in C. Ever program must have main
function to indicate, where the program begins its execution. When a
program to large and complex then the result of debugging testing
and maintaining becomes difficult.
7. UUssiinngg FFuunnccttiioonnss
• The main functions and other library functions does
need to be declared and defined but the main
function’s body need to be defined by the
programmer.
8. TThhee 33 ccoommppoonneennttss aassssoocciiaatteedd
wwiitthh ffuunnccttiioonnss aarree::
1. The Declaration
2. The function definition
3. The Calling Statement
9. FFuunnccttiioonn DDeeccllaarraattiioonn
• In C user- written functions should normally be
declared prior to its use to allow compiler to
perform type checking on arguments used in its call
statement.
• The general form is:
• Retirn_data_type function_name (data_type
Var_name, …..);
10. Function name: this is the name given to the
function. It follows the same naming convention as
that of any valid variable in C.
Return data type: this specifies the type of data
given back to the calling construct.
Data type list: this list specifies the data type of
each variables, the values of which are expected
to be transmitted to the function. These variables
are known as formal parameters.
11. NNoottee::
• It is possible not to declare functions prior to the use
but the error checking will not be performed.
• ; is required at the end of function declaration.
• E.g. int FindMax(int x, int y);
12. FFuunnccttiioonn DDeeffiinniittiioonn
The collection of program statements that does a
specific tasks done by the function is called the
function definition.
It conist of function header:
◦ Int FindMax(int x, int y)
◦ {
◦ }
and function body.
Int FindMax(int x, int y)
{
//body of the function….
}
13. FFLLOOWW OOFF FFUUNNCCTTIIOONN
• ●When the program is executed (that is, run) execution always begins at
• the first statement in the function main no matter where it is placed in the
• program.
• ●Other functions are executed only when they are called.
• ●Function prototypes appear before any function definition, so the
• compiler translates these first. The compiler can then correctly translate a
• function call.
• ●A function call statement results in the transfer of control to the first
• statement in the body of the called function.
• ●After the last statement of the called function is executed, the control is
• passed back to the point immediately following the function call.
• ●A value-returning function returns a value. Therefore, for value-returning
• functions, after executing the function when the control goes back to the
• caller, the value that the function returns replaces the function call
• statement.
14. FFuunnccttiioonn ccaallll
• The function is called from the main()
• The function can in turn call a another function.
• the function call statements invokes the function, which
means the program control passes to that function. Once the
function completes its task, the program control is passed
back to the calling environment.
• The general form of calling stmt is:
• Function_name (var1, var2,..);
• Or
• var_name=function name(var1, var2,..);
15. SSaalliieenntt ppooiinnttss ttoo bbee ttaakkeenn
iinnttoo ccoonnssiiddeerraattiioonn
• The func name and the type and number of
arguments must match with that of the function
declaration stmt and the header of the function
definition.
• Arguments present in the form of expression are
evaluated and converted to the type of formal
parameters at the beginning of the body of the
function.
16. PPaassssiinngg ooff aarrgguummeennttss ttoo
tthhee ffuunnccttiioonn
1. Call by value or pass by value:
1. When arguments are passed by values this means that local copies of
the values of the arguments are passed to the function.
2. Call by reference or pass by reference.
1. The address of the variable is passed as value of parameters to the
function.
17. Passing arrays to
functions
• Arrays can also be the arguments of function
• Only the base address of the array is passed to the
function
• Hence the passing of arguments is done by
reference.
• When arrays is passed as arguments then actual
contents of the arrays is altered.
18. RReeccuurrssiivvee FFuunnccttiioonnss..
• Recursion in programming is a technique for
defining a problem in terms of one or more smaller
versions of the problem.
• A recursive function is one that calls itself directly or
indirectly to solve a smaller version of its task until a
final call which does not require a self call.
19. TThhee mmeecchhaanniiccss ooff
rreeccuurrssiivvee ccaallll
• Start main program
• …..
• 1st call to print backward
• enter a char : H
o 2nd call to print_backward
• enter a char: i.
• 3rd call to print_backward
• enter a char: .
• //now it will not call againcoz its ‘.’
• 3rd call finish
• print i.
• 2nd call finish
• Print H.
First call Finish…
…
End of main program………
20. HHooww rreeccuurrssiioonn iiss
iimmpplleemmeenntteedd..
• The storage mechanism in most modern languages
is stack storage mgmt.
• In this mechanism the program’s data area is
allocated at load time.
• While storage for functions data is allocated when
function is invoked.
• On exit from function this storage is de-allocated.
• This results in a runtime stack.
• In recursive cases one call does not overlap with
the other.
21. WWhhaatt iiss nneeeedd ffoorr
iimmpplleemmeennttiinngg rreeccuurrssiioonn
• Decomposition into smaller problems of same size.
• Recursive call must diminish problem size.
• Necessity of base case.
• Base case must be reached.