Some examples of modular programming in C:
Example:
Design a modular program to add two numbers
#include<stdio.h>
/*
prototype */
int add (int x, int y);
int main()
{ int a, b, c;
a = 10; b = 20,;
c = add (a, b); /* function call */
printf(“\n The sum is : %d”, c);
return 0;
}
/*
function to add two integers
Input
: two integer x and y
Output :
sum of x and y
*/
int add (int x, int y)
{ z = x + y;
return (z);
}
NOTE
i.
Prototype means
the behavior of the function, i.e.
o
the return type,
o
the name
and,
o
the number and
type of input parameter
There is no need of
identifiers in the prototype. So, the following is also sufficient
int add (int,
int );
The prototype is also called “declaration” or
“header” .
ii.
The body of a
function that contains actual code for the function is called “definition”.
iii.
The definition of
a function can also be given before main() function. In this case there is no need of prototype.
iv.
The function main
is the “calling function”, and add() is
“called function”.
v.
In the calling
function, there is function call statement. The list inside parentheses is
called “argument list”.
vi.
In the header of
the called function, the list inside the parentheses is called the “parameter
list”.
vii.
The type and
number of arguments must exactly match with those parameter.
viii.
The value of
arguments is assigned to corresponding parameters.
int add ( int x, int y ) /* header of definition */
Effectively, it is an assignment operation: x = a; y = b;
The identifiers a,
b and c are called local identifier s of function main(). The identifiers x, y and z are called local identifier of add()
function. A local identifier can be used
within the function in which it is declared. So a, b and c are accessible
within main() only. Similarly x, y and z are accessible within add() function
only.
ix.
A function can
contain more than one return physically. But, logically only one will be
executed out of them. As soon as a return in the called function encounters,
execution control is transferred to the calling function at the point where a
function call was made.
c = add (a, b);
c = add (a, b);
Effectively, the returned value is assigned to the
lvalue of the assignment statement of the function call. So, the equivalent
effect is:
c = z
x.
The data type of
a following three must exactly match:
· The data type of return expression in called function.
· The return type of the function.
· The data type of variable, in which the returned value is being stored.
· The data type of return expression in called function.
· The return type of the function.
· The data type of variable, in which the returned value is being stored.
xi.
The returned
value can be discarded, if not required.
xii.
If the function
does not return anything, its return type is void. A void function may or may
have not return statement. If there is no return statement, it is implicitly
inserted at the end of function.
xiii.
Guideline:
Normally, a function should not contain input/output statement, because it
looses the generality of function. the input to the function should be supplied
as parameter. The result is normally returned from the function. but this
scheme has limitation, that we can return only single value from the function
multiple returned values can be done with the help of:
·
Structures
·
call by reference
parameter.
Example
/*
program to find factorial of a number */
#include<stdio.h>
long factorial(int); /* prototype */
int main()
{ int n;
long fact;
printf(“\nEnter a number : “);
scanf(“%d”, &n);
fact = factorial(n);
printf(“\n The factorial is : %ld”, fact);
return 0;
}
/*
function to find factorial
Input
: a number n
Output
: factorial of n
*/
long factorial (int num)
{ long fact = 1;
/* loop
to calculate factorial */
for (int I = 2; i<= num; ++i)
fact *= I;
return fact;
}
Formation of Stack:
The operating system
maintains a user stack to keep track of functions called. When a function is
called, a new entry on the top of stack is inserted, this entry contain the
local identifiers of the called function. When the function ends, this entry is
popped (removed). The behavior of function matches with property of STACK. The
function that is called at last will be completed first.
Example: Design a
function that takes and integer as parameter and returns its factorial.
Note: Local variables in two different functions may be of
same name, they are treated as independent variables. For example the variable
”fact” is local variable in the function main() and factorial() both. Both are
entirely different. So, the values will not conflict. It is simply because
separate space is allocated for “fact” of main() and “fact” of factorial().