Exercise: Create Function in C++


We've written this code:

void mainFunction() {
	cout << "Hello AlgoCademy!" << endl;
	cout << "Coding is amazing!" << endl;
	cout << "Functions are so useful!" << endl;

	cout << "Hello AlgoCademy!" << endl;
	cout << "Coding is amazing!" << endl;
	cout << "Functions are so useful!" << endl;

	cout << "Hello AlgoCademy!" << endl;
	cout << "Coding is amazing!" << endl;
	cout << "Functions are so useful!" << endl;
}

Write the same code in a better way by creating and calling a function printMessages()

Understanding the Problem

The core challenge here is to avoid code repetition by using functions. Functions help in making the code more modular, readable, and maintainable. This problem is significant because it teaches the importance of code reusability and modularity, which are fundamental principles in software development.

Approach

To solve this problem, we need to identify the repeated code and encapsulate it within a function. This function can then be called multiple times, reducing redundancy.

Naive Solution

The naive solution is the given code itself, where the same set of print statements is repeated multiple times. This approach is not optimal because it leads to code duplication, making the code harder to maintain and error-prone.

Optimized Solution

The optimized solution involves creating a function printMessages() that contains the repeated print statements. We then call this function multiple times in the mainFunction().

Algorithm

Here is a step-by-step breakdown of the optimized solution:

  1. Create a function printMessages() that contains the repeated print statements.
  2. In the mainFunction(), call the printMessages() function three times.

Code Implementation


#include <iostream>
using namespace std;

// Function to print the messages
void printMessages() {
    cout << "Hello AlgoCademy!" << endl;
    cout << "Coding is amazing!" << endl;
    cout << "Functions are so useful!" << endl;
}

// Main function
void mainFunction() {
    // Call the printMessages function three times
    printMessages();
    printMessages();
    printMessages();
}

int main() {
    mainFunction();
    return 0;
}

Complexity Analysis

The time complexity of this solution is O(1) because the number of operations is constant and does not depend on any input size. The space complexity is also O(1) as we are not using any additional data structures that grow with input size.

Edge Cases

There are no significant edge cases for this problem as it involves simple print statements. However, if the print statements were dependent on some input, we would need to consider various input scenarios.

Testing

To test this solution, simply run the program and verify that the output matches the expected repeated messages. No special testing frameworks are needed for this simple problem.

Thinking and Problem-Solving Tips

When faced with repetitive code, always consider using functions to encapsulate the repeated logic. This not only makes your code cleaner but also easier to maintain and debug. Practice by identifying repetitive patterns in your code and refactoring them into functions.

Conclusion

In this exercise, we learned how to refactor repetitive code by using functions. This approach makes the code more modular and maintainable. Understanding and applying such principles is crucial for writing efficient and clean code.

Additional Resources