Modular programming, Embedded Programming Chapter 5

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.

Click here for Previous chapters

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 :

  1. Function can call another function
  2. It can return data to the kowler tree
  3. Function can accept data from the kowler
  4. It makes the program modular, easy to understand and simple to modify
  5. Function are integral part of any kind of software development
  6. Function allows the development work to be distributed among the team of programmers
  7. Function adds the reusability value thus it makes the source code compact

click here for next chapter

Click here for Previous chapters

Embedded C programming Tutorial : click below links to learn more

chapter 1         chapter 2        chapter 3        chapter 4        chapter 5      Chapter 6

chapter 7