Computer Concept & Programming In C - Unit III - 15

Q.31 What is modular programming? Discuss in detail.
Related Questions -
Q. Write a short note on GOTO statement.                             (AKTU. 2011 - 12)
Ans. Modular Programming: -
It is not a good programming practice to try to solve a complex problem with a single function. The proper way to approach it is to break the problem into several smaller pieces that can be understood in more details and then start to define and build functions to solve those smaller and simpler problems. Keep in mind that each of your functions should do only one task, but do it well.
As your program continues to grow, you should consider breaking it into several source files, with each source file containing a small group of cohesive functions. Such source files are also called modules. Put data declaration and functions or prototypes into header files, so that any changes to the declarations or prototypes can be automatically signified to all source files that include the header file.
* Separate compilation and linkage: -
The C compiler enables you to compile and debug different source files separately. In this way you can focus on one-source file at a time and complete the compiling before you move to the next one. With the separate compilation you can compile only those source files that have changed and leave the source files that have already been compile and debugged and unattached.
Compilation and linkage: -
First a program written in C, called source code is made. Then the source code is compiled by a C compiler, which creates a new file. The new file is an object file.
You cannot execute the object file because there is some function code missing. You have to finish the next step: linking, linking is done by invoking a special program called a linker, which normally comes with the compiler package.
A linker is used to link together the object file, the ANSI standard C library, and other user generated libraries to produce an executable file-the binary code. In this stage the binary code of the library functions that are called in the source code is combined with the object file; the result is saved into a new file an executable file.
Scope rules and global variables: -
Limiting the scope of variables is very useful when several programmers are working an different pieces of the same program. If they limit the scope of their variables to their pieces of code they do not have to worry about conflicting with variables of the same name used by other in other parts of the program.
In C, you can declare a variable and indicate its visibility level by designating its scope. Thus variables with local scope can only be accessed within the block in which they are declared only be accessed within the block in which they are declared.
Block Scope: -
A block refers to any sets of statements enclosed in braces ({and}). A variable declared within a block has block scope. Thus the variable is active and accessible from its declaration point to the end of the block. Sometimes a block scope is also called a local scope. For example, the variable i declared within the block of the following main function has block scope:
int main ( )       
{
int i; /*block scope*/
  return o;
}
Usually a variable with block scope is called a local variable.
Nested Block Scope: -
You can also declare variables within a nested block. If a variable declared in the outer block shares the same name with one of the variables in the inner block, the variable within the outer block is hidden by the one within inner block.
Function Scope: -
Function scope indicates that a variable is active and visible from the beginning to the end of a function.
In C, only the goto label has function scope. For example, the (goto) label, start, shown in the following code portion has a function scope:
int main ( )
{
int i; /* block scope*/
start: /* A goto label function scope */
goto start; /* the goto statement */
return o;
}
Here, the label start is visible form the beginning to the end of the main ( ) function. Therefore, there should not be more than one label having the same name within the main ( ) function.
Program scope/global variable: -
A variable is said to have program scope when it is declared outside a function. For instance the following code. 
int x = 0; /* program scope*/
float y = 3.14 /* program scope*/
void main ( ) 
{
int i; /* block scope*/
return 0;
}
Here, the int variable x and float variable y have program scope.
Variables with program scope are also called global variables, which are visible among different files. These files are the entire source files that make up an executable program. Note that a global variable is declared with an intializer outside a function.
Remember: -
Since a global variable is visible among different source files of a program, using global variables increases your program’s complexity, which in turn makes your program hard to maintain and debug. Generally, it is not recommended that you declare and use global variables, unless it is very necessary.

Q.32. Explain, building your own modulus & passing arguments by value with the help of a C program.
Ans. Building Your Own Modules & passing Arguments By Value: - 
Consider the following program
# include <stdio.h>
double add (double x, double y);
double substract (double x, double y);
{
printf (“\n Within substract function”);
return (x - y);
}
void main ( )
{
double x1 = 10, x2 = 20, x3 = 30, x4 = 40;
printf (“\n Pass add %lf and %lf ”, x1, x2);
printf (“\n add returns %lf ”, add (x1, x2);
printf (“\ n pass substract %lf and %lf ”, x3, x4);
printf (“\n substract returns %lf ”, substract (x3, x4);
}
double add (double x, double y);
{
printf (“\ n within add function”);
return (x + y);
}
The output of the preceding program is:
Explanation of the preceding program: -
The purpose of the above program is to show you how to declare and define functions. The functions are individual modules of modular programming.
The statement double add (double x, double y), is a function declaration with a prototype. The declaration alludes to the function add defined at the end. The return type of the add is double, and the function prototype includes two double variables x and y called argument to the function.
Note: -
Both functions add and substract shares the same variables x and y of type double as arguments. There is no Harm in doing so because these arguments are in different function blocks.
The line double x1 = 10, x2 = 20, x3 = 30, x4 = 40, initializes the variables x1, x2, x3, x4 of type double with the values 10, 20, 30, and 40 respectively. The line printf (“\ n pass add %lf ” and %lf ”, x1, x2); prints the massage pass add 10 and 20. (Note: %lf are format specifiers of type double or long float). The line printf (“\ n add returns %lf”, add (x1, x2)); actually calls the method add with argument x1 = 10 and x2 = 20 respectively and then prints the message, within add function and then returns (x + y) initially the arguments get stored in x and y and x1 = 10 at x & x2 = 20 at y.
Similarly the function substracts works in the next two lines.
At the end the function add is defined as follows:
1. double add (double x, double y)
2. {
3. printf (“\n within add function”);
4. return (x + y);
5. }
In line 1 the double add (double x, double y) tells 
(i) The function add whenever called returns a double value. By double add.
(ii) The function accepts two input arguments of type double and store them in x  and y respectively by add (double x, double y).
In line 2 the body of function starts.
In line 3 a simple massage is printed.
In line 4 computations are done according to the formula within (x + y) and the value return the value after computations.
In line 5 the body of the function adds is closed.