Friday, 10 June 2016

Modular programming in C

Modular programming is a powerful technique which offers many advantages. consider an analogy for it, uppose a task is given to you to clean a 10 storey building. Its obvious that you are not going to do it all alone. suppose you hire a group of 10  people such that each will clean each storey. It may also possible that those two may hire other for their help and so on. What you did is divided a big task into smaller one and those smaller ones may further be broken in to subtask and so on. Same happens in C also and these subtasks are known as module. A C program can be broken up into modules and finally combined back. This is what we call modular programming.

The essential component of modular programming is functions. A good knowledge of function is essential for a C programmer.

Functions

As name implies function does some task just as a  person do. 
 There are some terms associated with functions
1.Function name:name of function, it should denote what function does
2.Function parameter: the values needed by a function to perform a task upon, also called arguments.
3.Function type: the value that a function give after its execution also called return type of function because function returns it.
4.Function body: task or tasks that a funtion perform
5.Function return : A function may return or may not return something, whatever it is it should be written in return in  function body

These functions are called by main program

Remember the following three point to use a function in a  program
1.A function can only be used if it has been declared. Declaration is done before main. Just like a war can happen only if it is declared. Declaration only mean I am going to use this function.

2. A function has to be define, Function should be define outside the main program. It really does not matter where but should be outside the main.

3.Function use: function should be used inside the main

Lets code something than only we will understand what does I mean

open a file sudo nano modprog.c and paste following code and save it

#include <stdio.h>

//declaration of functions which we are going to use in our program
void Printline();//prints  line
void printdot();//prints  dot
void printstar();//prints star


//Defining the print line function

void printline(){
printf("___________________________________________\n");
printdot();
printstar();
}

//Defining the printdot function
void printdot(){
printf("............................................\n");
}

//Defining the printstar function
void printstar(){
printf("********************************************\n");
}

//declaring a global variable that can be used any where in this program
int i;

//main program
int main()
{for ( i=0;i<5;i++)
printline();
}

Explanation.

Start withthe main program,Here a loop is going on 5 times and the print line function is called. Now go to the print line function where it is defined. This printline function prints lin and then call print dot funtion, Now go where the print dot is define, print dot function prints dot After printindot it return to  print line . Printline function then call print star function, print star then prin star after printing star, there is nothing to do for a printline so printline function return to the main program. The output is

............................................
********************************************
___________________________________________
............................................
********************************************
___________________________________________
............................................
********************************************
___________________________________________
............................................
********************************************
___________________________________________
............................................
********************************************


Lets make function to accept something so that it can perform some operations on it. This is called passing parameteor arguments to function. Main program gives parameter to function, hence this parameter is calle actual parameter. These parameter are mapped to the parameters defined inside our function, The parameters inside the function is called as formal parameter. Function operates on formal parameters and gives back the result, its called return of function

We have seen simple function thatdoes not except any parameter from main. Let make function more usable by passing some parameter to it. Remember the parameter during function call is called actual parameter and the parameter in function definition called formal parameter. Both actual and formal parameter should be of same type. The program below explain this phenomenon

#include<stdio.h>

//function declaration

int add(int a, int b);

//main program

int main(){
int y;

//function call
y=add(2,3);
printf("y is %d\n", y);
}


//function definition
add(int a, int b){

return (a+b);
}


Here the function return the addition of a and b and the value of a and b comes from main program which calls this add function.

One question can be pop up in mind that is it possible that a function can return multiple reults from a function? try to run the following program





#include<stdio.h>

//function declaration
int add(int a, int b);

//main program
int main(){
int y;

//function call


y=add(2,3);
printf("y is %d\n", y);
}

//function definition

add(int a, int b){
return (a-b);
return (a+b);
}


here add function tries to return two value to main, but it is unable to returning a+b. One thing is clear that this approach is not correct. There is a way to accomplish this and this is done by telling the function that you should return more than one value, the parameter which are used by function for outputting more than one result is called output parameters. so output parameter wiil solve our problem.

To use output parameters we must also mention  about these parameter in every where that is in function definition, function declaration and in function call also.

#include<stdio.h>

//function declaration, notice here the mentioning of output //parameters, Here s and d are not
//normal variable, these are pointers means they store the address of //some variable


int add(int a, int b, int *s, int *d);

//main program

int main(){

//declaring variables, these are normal variable and occupied a location //in memory
//and that location has an address

int s,d;

//function call, note here that we are giving address of s and d to add function

add(20,10,&s,&d);
printf("the value of s and d are %d and %d\n", s,d);
}
//function definition, Here add fuction accepts 20 in a, 10 in b and //output the value stored in
// location specified by s and  output the value stored in d.

add(int a, int b, int *s, int *d){

*s=a+b;
*d=a-b;
}


In simple words we gives address to the function and and the function modifies the value in that address and we are printing the value thats it. Not giving the value but giving the address to the function is called calling the function by reference

Lets us give array to the function, Following program illustrates it

#include<stdio.h>

//Function declaration, Here our function is expecting array from main
int add_all(int a[]);

int main(){
// the array which is to given to function
int a[]={2,3,4,5,6,7,8};

int result;

//function call, Note here that the name of araray is only be used inside //functiion

result=add_all(a);

printf("result is %d", result);

}

//function declaration, Mentioning that function will accept the array

int add_all(int a[]){
int i;
int res=0;
for (i=0;i<7;++i){
res+=a[i];
}
return res;
}



To give array to function remember, function is called by only the name of array and bot function definition and function declaration muxt mention that function is expecting array from main program. similar concept can be expanded to two dimensional array.


we have seen how to pass a value or an array to function, we have seen what does modular programming approach means. But here comes the most important part of modular programming, Suppose your are a part of a team and a task is given to that team and each team member writes it own code, one of your friend writes different function and you want to use those function in your code but both of you has separate file, So How to do this?



we are breaking the above code into three files
sudo nano a.c
sudo nano b.c
sudo nano c.h

In c.h write following code

//this file is called header file and contain function declaration and //macro definitions.
int add_all(int a[]);


in a.c write the following code


//This file contain the main program
#include<stdio.h>

//we had made c.h and we are including that here
#include "c.h"

//Function declaration, Here our function is expecting array from main

int main(){
// the array which is to given to function

int a[]={2,3,4,5,6,7,8};

int result;

//function call, Note here that the name of araray is only be used //insidefunctiion

result=add_all(a);

printf("result is %d", result);

}



in b.c write following codes

//This file contains function definition

//function declaration, Mentioning that function will accept the array

int add_all(int a[]){
int i;
int res=0;
for (i=0;i<7;++i){
res+=a[i];
}
return res;
}


 We need to link and compile only two file a.c and b.c , do it by following commands

gcc a.c b.c

this will generate executable, with name a.out run that exxecutable with

sudo ./a.out





We have seen how to make use of modular programming and make coding flexible and portable



No comments:

Post a Comment

Comments System

Disqus Shortname