- 1 what is a modular programming ?
- 2 How can we implement modular programming in C ?
- 3 Key point in Modular Programming
- 4 How to write Function for Modular Programming ?
- 5 why should we use functions :
- 6 Click here for Previous chapters
what is a modular programming ?
I will try to explain it using one real-life example. You use mp3 players, cell phones or any other gadget. you recharge the battery if it goes down. You replace it after every year so that you can get maximum usage time. Imagine what happens if you have to throw a complete gadget. Because your battery has gone down. Would you buy such products, I am sure you will not replacement of parts like battery. Display screen or wheel of your car is possible to replace due to the modular design of the system. Modular design helps us to achieve this easy replacement and up gradation.
How can we implement modular programming in C ?
Let’s see how can we implement modular programming in C language. Modular programming is breaking up a program into segments. Each of which can be written more or less independently of the others in C. These segments are called functions. Functions are a section of codes written to perform dedicated task. For example get temperature, get user key etc.
The word function has appeared a few times so far with reference to main printf function body and so on.
Key point in Modular Programming
Let explore in a little more depth what functions are and why they important. The image displays the modular programming approach in C language. There are three function used which are called by main function. Imagine this is a program (talking abvout above image) as a loan repayment schedule calculator. Here function 1 accepts loan amount and function 2 to process the data. function 3 is used to display the calculated results to the user screen. Now in future if interest rates are changed it will affect loan repayment calculation. Thus due to the modular approach we need to update the function to make the necessary change.
How to write Function for Modular Programming ?
Modular programming also allows us to divide the project development work in a team of programmers. Let’s move ahead to learn the syntax of writing functions for modular programming. Look at the below program. We have written a function named GetTemperature. The total syntax of writing a new function is indicated by blue text.
This indicates the data type of the function
This indicates the name of new function
This place is reserved for accepting values from the calling functions
This place is reserved to indicate if this function returns some value to the kowler.
Return statement is used to return the value to the kowler here we have variable data. Which has the temperature value. This value will be returned to the kowler
The above figure with arrow mark shows a call to new function get temperature. The value returned by the get temperature function will be stored in a variable named temperature.
Below program calculates sum of two numbers in the main function.
we have assigned values to variable a and b. These values are then passed to the function called sum. Sum function receives the values and accepts the value of a into X and value of b into y. The sum function process the calculation and returns the calculated value to the kowler. The return value is stored in variable names. For further processing as we know this place is reserved to indicate that the function returns some value. Here in this example function sum returns a value of integer datatype to the kowler.
why should we use functions :
- Function can call another function
- It can return data to the kowler tree
- Function can accept data from the kowler
- It makes the program modular, easy to understand and simple to modify
- Function are integral part of any kind of software development
- Function allows the development work to be distributed among the team of programmers
- Function adds the reusability value thus it makes the source code compact