Course: CS-303 Programming Fundamentals
Topics: Combining Functions, Modular Programming, Practical Problem-Solving and Function-Based Programs
After completing this lecture, students will be able to:
We have learned the following concepts:
| Lecture | Topics |
|---|---|
| Lecture 1 | Introduction to Functions |
| Lecture 2 | Function Parameters and Return Values |
| Lecture 3 | Function Prototypes, Variable Scope and Parameter Passing |
| Lecture 4 | Default Arguments, Function Overloading and Recursion |
Now we will apply these concepts to solve practical programming problems.
Modular programming means dividing a large program into smaller, manageable parts called modules or functions.
Each function performs a specific task.
Real-life example: University Student Management System
A university system may perform several operations:
Instead of writing all instructions inside main(), we can create separate functions for each operation.
Student Management System
main()
Controls the program
inputMarks()
Input marks
calculateTotal()
Calculate total
calculatePercentage()
Calculate percentage
displayResult()
Display result
Problem Statement: Write a C++ program that accepts marks of three subjects, calculates total marks and percentage, and displays the result using functions.
The program needs to perform four tasks:
| Function | Purpose |
|---|---|
inputMarks() |
Accept three subject marks |
calculateTotal() |
Calculate total marks |
calculatePercentage() |
Calculate percentage |
displayResult() |
Display final result |
#include <iostream>
using namespace std;
// Function prototypes
void inputMarks(int &m1, int &m2, int &m3);
int calculateTotal(int m1, int m2, int m3);
double calculatePercentage(int total);
void displayResult(int total, double percentage);
int main()
{
int m1, m2, m3;
inputMarks(m1, m2, m3);
int total = calculateTotal(m1, m2, m3);
double percentage = calculatePercentage(total);
displayResult(total, percentage);
return 0;
}
// Input marks
void inputMarks(int &m1, int &m2, int &m3)
{
cout << "Enter marks of three subjects: ";
cin >> m1 >> m2 >> m3;
}
// Calculate total
int calculateTotal(int m1, int m2, int m3)
{
return m1 + m2 + m3;
}
// Calculate percentage
double calculatePercentage(int total)
{
return (total / 300.0) * 100;
}
// Display result
void displayResult(int total, double percentage)
{
cout << "Total Marks: " << total << endl;
cout << "Percentage: " << percentage << "%" << endl;
}
Enter marks of three subjects: 75 80 85
Total Marks: 240
Percentage: 80%
main().inputMarks() accepts marks from the user.calculateTotal() calculates the total marks.calculatePercentage() calculates the percentage.displayResult() displays the final result.Important concept: The & symbol in inputMarks() allows the function to modify the original variables in main() through reference parameters.
Problem Statement: Write a C++ program that performs addition, subtraction, multiplication and division using separate functions.
| Function | Operation |
|---|---|
add() |
Addition |
subtract() |
Subtraction |
multiply() |
Multiplication |
divide() |
Division |
#include <iostream>
using namespace std;
int add(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int multiply(int a, int b)
{
return a * b;
}
double divide(double a, double b)
{
return a / b;
}
int main()
{
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Addition: " << add(num1, num2) << endl;
cout << "Subtraction: " << subtract(num1, num2) << endl;
cout << "Multiplication: " << multiply(num1, num2) << endl;
if (num2 != 0)
{
cout << "Division: " << divide(num1, num2) << endl;
}
else
{
cout << "Division by zero is not allowed." << endl;
}
return 0;
}
Enter two numbers: 20 10
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
Ask students:
divide() function use double?We can use function overloading to calculate the area of different shapes.
Problem Statement: Write a C++ program using overloaded functions to calculate the area of a square, rectangle and circle.
#include <iostream>
using namespace std;
double area(double side)
{
return side * side;
}
double area(double length, double width)
{
return length * width;
}
double area(int radius, double pi)
{
return pi * radius * radius;
}
int main()
{
cout << "Square Area: " << area(5.0) << endl;
cout << "Rectangle Area: " << area(5.0, 4.0) << endl;
cout << "Circle Area: " << area(3, 3.14159) << endl;
return 0;
}
Output:
Square Area: 25
Rectangle Area: 20
Circle Area: 28.2743
Observation: All three functions have the same name, area(), but their parameter lists are different.
Problem Statement: Write a recursive function to calculate the sum of integers from 1 to n.
#include <iostream>
using namespace std;
int calculateSum(int n)
{
if (n == 0)
return 0;
return n + calculateSum(n - 1);
}
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number < 0)
{
cout << "Please enter a non-negative number.";
}
else
{
cout << "Sum = " << calculateSum(number);
}
return 0;
}
Output:
Enter a number: 5
Sum = 15
For calculateSum(4):
| Function Call | Calculation | Return Value |
|---|---|---|
calculateSum(4) |
4 + calculateSum(3) | 10 |
calculateSum(3) |
3 + calculateSum(2) | 6 |
calculateSum(2) |
2 + calculateSum(1) | 3 |
calculateSum(1) |
1 + calculateSum(0) | 1 |
calculateSum(0) |
Base case | 0 |
Final result:
\[ 4+3+2+1+0=10 \]
Students should complete the following problems in the computer lab.
Write a C++ program using functions to calculate an employee’s salary.
Tasks:
calculateAllowance() to calculate 20% allowance.calculateTax() to calculate 5% tax on basic salary.calculateNetSalary() to calculate net salary.Requirements:
Write a C++ program using functions to check different properties of a number.
Tasks:
isEven() to check whether a number is even.isPositive() to check whether a number is positive.isPrime() to check whether a number is prime.main().Requirements:
bool return types where appropriate.Write a C++ program to calculate a student’s grade using functions.
Tasks:
Requirements:
if-else statements for grade calculation.Ask students to modify the programs.
| No. | Modification Task |
|---|---|
| 1 | Add a function to calculate the average of three numbers. |
| 2 | Modify the calculator to use a menu. |
| 3 | Add a function to check whether a year is a leap year. |
| 4 | Modify the student result program to accept five subjects. |
| 5 | Calculate factorial using both recursion and iteration. |
Test your understanding
0 / 5 answered
1. What is modular programming?
A. Writing all code in main()
B. Dividing a program into smaller functions
C. Using only global variables
D. Avoiding functions
2. Which function usually controls the execution of a C++ program?
A. add()
B. display()
C. main()
D. calculate()
3. Which return type is suitable for a function that checks whether a number is even?
A. void
B. bool
C. string
D. char
4. What is the main advantage of separate functions?
A. More repetition
B. Code becomes harder to test
C. Better organization and reuse
D. No need for variables
5. What happens when a function returns a value?
A. It sends a result back to the calling code
B. It always ends the entire program
C. It creates a global variable
D. It starts main() again
Submit Answers
In this lecture, we applied C++ functions to practical programming problems.
| Concept | Practical Application |
|---|---|
| Multiple functions | Student result system |
| Parameters and return values | Calculator |
| Function overloading | Area calculation |
| Recursion | Sum of numbers |
| Modular programming | Employee and student management |
Key Message for Students:
A good programmer does not write the entire program in one place. A good programmer divides a problem into smaller tasks and creates separate functions to solve those tasks.