We've written this code:
void mainFunction() {
cout << "My name is Andy" << endl;
cout << "I code in Python" << endl;
cout << "I have a dog" << endl;
cout << "My name is Mike" << endl;
cout << "I code in Java" << endl;
cout << "I have a cat" << endl;
cout << "My name is Sarah" << endl;
cout << "I code in JavaScript" << endl;
cout << "I have a parrot" << endl;
}
Write the same code in a better way by creating and calling a function with parameters printMessages()
The core challenge here is to avoid repetitive code by using a function with parameters. This makes the code more modular, readable, and maintainable. Functions with parameters are commonly used in programming to handle repetitive tasks with varying inputs.
To solve this problem, we can create a function named printMessages()
that takes three parameters: the name, the programming language, and the pet. This function will then print the messages using these parameters. This approach eliminates redundancy and makes the code cleaner.
The initial code is repetitive and not modular. It prints the same type of messages for different inputs without using a function. This is not optimal because any change in the message format would require changes in multiple places.
We can create a function printMessages()
that takes three parameters and prints the messages. This way, we only need to call this function with different arguments to achieve the same result.
Here is a step-by-step breakdown of the optimized solution:
printMessages()
that takes three parameters: name
, language
, and pet
.cout
to print the messages using the parameters.mainFunction()
, call printMessages()
with different sets of arguments.#include <iostream>
using namespace std;
// Function to print messages
void printMessages(string name, string language, string pet) {
cout << "My name is " << name << endl;
cout << "I code in " << language << endl;
cout << "I have a " << pet << endl;
}
void mainFunction() {
// Calling the function with different arguments
printMessages("Andy", "Python", "dog");
printMessages("Mike", "Java", "cat");
printMessages("Sarah", "JavaScript", "parrot");
}
int main() {
mainFunction();
return 0;
}
The time complexity of this solution is O(1) because the number of operations is constant and does not depend on the input size. The space complexity is also O(1) as we are not using any additional data structures that grow with input size.
Potential edge cases include:
These edge cases are handled naturally by the function as it simply prints whatever is passed to it.
To test the solution comprehensively, you can use a variety of test cases:
Using a testing framework like Google Test can help automate and manage these tests.
When approaching such problems, always look for patterns and repetitive code that can be modularized. Functions with parameters are a powerful tool to make your code more maintainable and readable. Practice by identifying repetitive tasks in your code and refactoring them into functions.
In this exercise, we learned how to refactor repetitive code by using a function with parameters. This approach makes the code cleaner, more modular, and easier to maintain. Understanding and applying such techniques is crucial for writing efficient and maintainable code.