📖 Scenario: Imagine you are building a simple calculator program in C. Instead of writing all the code in one big block, you want to organize it into smaller parts. This helps you understand, fix, and improve your program easily.
🎯 Goal: You will create a small C program that uses modular programming by dividing the calculator tasks into separate functions. This will show why modular programming is helpful.
📋 What You'll Learn
Create a function called add that takes two integers and returns their sum.
Create a function called subtract that takes two integers and returns their difference.
Create a function called multiply that takes two integers and returns their product.
Create a function called divide that takes two integers and returns their quotient as a float.
Use these functions in the main function to perform calculations and print the results.
💡 Why This Matters
🌍 Real World
Modular programming is used in real software projects to keep code organized and easy to maintain, especially when many people work together.
💼 Career
Understanding modular programming is essential for software developers to write clean, reusable, and scalable code.
Progress0 / 4 steps
1
Create the add function
Write a function called add that takes two integers named a and b and returns their sum as an integer.
C
Hint
Define a function with int add(int a, int b) and return a + b.
2
Create the subtract function
Write a function called subtract that takes two integers named a and b and returns their difference as an integer.
C
Hint
Define a function with int subtract(int a, int b) and return a - b.
3
Create the multiply and divide functions
Write two functions: multiply that takes two integers a and b and returns their product as an integer, and divide that takes two integers a and b and returns their quotient as a float.
C
Hint
Define multiply to return a * b and divide to return (float)a / b.
4
Use the functions in main and print results
In the main function, call add(10, 5), subtract(10, 5), multiply(10, 5), and divide(10, 5). Print each result on its own line with printf.
C
Hint
Use printf to print each result with labels and newline characters.
Practice
(1/5)
1. Why is modular programming important in C programming?
easy
A. It makes the program run faster automatically
B. It prevents the use of functions
C. It removes the need for comments in code
D. It breaks a large program into smaller, manageable parts
Solution
Step 1: Understand modular programming concept
Modular programming divides a big program into smaller parts called modules.
Step 2: Identify the benefit of breaking code
Breaking code into parts makes it easier to manage and understand.
Final Answer:
It breaks a large program into smaller, manageable parts -> Option D
Quick Check:
Modular programming = breaking code into parts [OK]
Hint: Modular means dividing big code into small parts [OK]
Common Mistakes:
Thinking modular programming speeds up code automatically
Believing modular programming removes comments
Confusing modular programming with removing functions
2. Which of the following is the correct way to include a module in a C program?
easy
A. import module;
B. #include "module.c"
C. #include
D. use module;
Solution
Step 1: Recall C syntax for including modules
C uses #include directive to include header files with .h extension.
Step 2: Identify correct syntax
#include is the standard way to include a module's header file.
Final Answer:
#include <module.h> -> Option C
Quick Check:
#include <header.h> is correct [OK]
Hint: Use #include with .h files for modules in C [OK]
Common Mistakes:
Including .c files directly with #include
Using import or use keywords which are not in C
Forgetting angle brackets or quotes in #include
3. Consider this C program split into two files: main.c and math_utils.c.
// math_utils.c int add(int a, int b) { return a + b; }
// main.c #include int add(int, int); int main() { int result = add(3, 4); printf("%d", result); return 0; }
What is the output when compiled and run correctly?
medium
A. 0
B. 7
C. 34
D. Compilation error
Solution
Step 1: Understand function call across modules
The function add is defined in math_utils.c and declared in main.c, so calling add(3,4) returns 7.
Step 2: Check output of printf
printf prints the integer result, which is 7.
Final Answer:
7 -> Option B
Quick Check:
3 + 4 = 7 [OK]
Hint: Function add returns sum; 3+4=7 output [OK]
Common Mistakes:
Expecting concatenation instead of addition
Missing function declaration causing errors
Confusing output with zero or error
4. This code tries to use modular programming but has an error: