M
FUNCTION
MONOLITHIC VS MODULAR PROGRAMMING:
1. Monolithic Programming indicates the program which contains a single function for the large program.
2. Modular programming help the programmer to divide the whole program into different modules and each module is separately developed and tested. Then the linker
will link all these modules to form the complete program.
3. On the other hand monolithic programming will not divide the program and it is a single thread of execution. When the program size increases it leads inconvenience and
difficult to maintain.
Disadvantages of monolithic programming: 1. Difficult to check error on large programs. 2. Difficult to maintain. 3. Code can be specific to a particular problem. i.e. it can not be
reused.
Advantage of modular programming: 1. Modular program are easier to code and debug. 2. Reduces the programming size. 3. Code can be reused in other programs. 4.
Problem can be isolated to specific module so easier to find the error and correct it.
FUNCTION:
A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional
functions.
Function Declaration OR Function Prototype:
1. It is also known as function prototype .
2. It inform the computer about the three things
a) Name of the function
b) Number and type of arguments received by the function.
c) Type of value return by the function
Syntax :
return_type function_name (type1 arg1 , type2 arg2);
OR
return_type function_name (type1 type2);
3. Calling function need information about called function .If called function is place
before calling function then the declaration is not needed.
Function Definition:
1. It consists of code description and code of a function .
It consists of two parts
a) Function header
b) Function coding
Function definition tells what are the I/O function and what is
going to do. Syntax:
return_type function_name (type1 arg1 , type2 arg2)
{
local variable;
statements ;
return (expression);
2. Function definition can be placed any where in the program but generally placed after
the main function .
3. Local variable declared inside the function is local to that function. It cannot be used
anywhere in the program and its existence is only within the function.
4. Function definition cannot be nested.
5. Return type denote the type of value that function will return and return type is optional
if omitted it is assumed to be integer by default.
USER DEFINE FUNCTIONS VS STANDARD FUNCTION:
User Define Fuction:
LECTURE NOTE 14
FUNCTION CATAGORIES
There are four main categories of the functions these are as follows:
1. Function with no arguments and no return values.
2. Function with no arguments and a return value.
3. Function with arguments and no return values.
4. Function with arguments and return values.
Function with no arguments and no return values:
syntax:
d
Function with no arguments and a return value: This type of functions has no arguments but
a return value
example:
int msg
(void) ;
int main
()
{
int s =
msg ( );
printf(
“summa
tion =
%d” ,
s);
}
int
NOTE: Here msg called function is independent, it read the value from the keyboard, initialize and
( void )
return a value .Both calling and called function are partly communicated with each other.
{
int a,
Function with
b, arguments and no return values:
sum ;
Here functions have arguments so, calling function send data to called function but
sum
called function
= does no return value. such functions are partly dependent on calling function and
a+b ;
result obtained is utilized by called function .
retur
n
Example:
(sum)
; v
}
o
d
LECTURE NOTE 15
ACTUAL ARGUMENTS AND FORMAL ARGUMENTS
Actual Arguments:
1. Arguments which are mentioned in the function in the function call are known as
calling function.
2. These are the values which are actual arguments called to the function.
It can be written as constant , function expression on any function call which return a value .
ex: funct (6,9) , funct ( a,b )
Formal Arguments:
1. Arguments which are mentioned in function definition are called dummy or
formal argument.
2. These arguments are used to just hold the value that is sent by calling function.
3. Formal arguments are like other local variables of the function which are created
when function call starts and destroyed when end function.
Basic difference between formal and local argument are:
a) Formal arguments are declared within the ( ) where as local
variables are declared at beginning.
b) Formal arguments are automatically initialized when a value of
actual argument is passed.
Note: Order, number and type of actual argument in the function call should be matched with the
order , numberc)and type of formal
Where other local
arguments variables
in the aredefinition
function assigned .variable through the
statement inside the function body.
PARAMETER PASSING TECHNIQUES:
1. call by value
2. call by reference
Call by value:
Here value of actual arguments is passed to the formal arguments and operation is
done in the formal argument.
Since formal arguments are photo copy of actual argument, any change of the formal arguments
does not affect the actual arguments
Example:
Changes made to the formal argument t are local to block of called function, so when control back
void swap (int achanges /* called function */
, int made
to calling function vanish.
b)
int t;
t
} =
main( )
a
{
;
int k = 50,m= 25;
swap(
a k, m) ; / * calling
function
= */ print (k, m);
b / * calling
function */
;
}
b
Output:
Explanation:
50, 25
=int k= 50, m=25 ;
Means first two memory space are created k and m , store the values 50 and 25 respectively.
t
;
LECTURE NOTE 16
RECURSION
Recursion is a process in which a problem is defined in terms of itself. In ‘C’ it is possible to call
a function from itself. Functions that call themselves are known as recursive functions, i.e.
a statement within the body of a function calls the same function. Recursion is often termed as
‘Circular Definition’. Thus recursion is the process of defining something in terms of itself. To
implement recursion technique in programming, a function should be capable of calling itself.
Example:void main()
…………………… /* Some statements*/
fun1();
……………………… /* Some statements */
}
v
{
o
……………………… /* Some statements */ fun1();
i
/*RECURSIVE
d CALL*/
……………………… /* Some statements */
}f
u
Here then function fun1() is calling itself inside its own function body, so fun1() is a
recursive function. When main() calls fun1(), the code of fun1() will be executed and since there
is a call 1to fun1() insidefun1(), again fun1() will be executed. It looks like the above program
will run up to infinite times but generally a terminating condition is written inside the recursive
functions( which end this recursion. The following program (which is used to print all the
numbers )starting from the given number to 1 with successive decrement by 1) illustrates this: