Start learning free

LESSON 39 OF 409 · FUNCTIONS I

Exercise: Create Function with Parameters in C++

Practice Read the full explanation

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()

STEP 1 OF 5
    Ctrl + Enter to check

    C++ · FUNCTIONS I

    Exercise Create Function With Parameters in C++, explained

    Understanding the Problem

    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.

    Approach

    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.

    Initial Naive Solution

    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.

    Optimized Solution

    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.

    Algorithm

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

    1. Create a function printMessages() that takes three parameters: name, language, and pet.
    2. Inside the function, use cout to print the messages using the parameters.
    3. In the mainFunction(), call printMessages() with different sets of arguments.

    Code Implementation

    #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;
    }
    

    Complexity Analysis

    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.

    Edge Cases

    Potential edge cases include:

    • Empty strings for name, language, or pet.
    • Very long strings for any of the parameters.

    These edge cases are handled naturally by the function as it simply prints whatever is passed to it.

    Testing

    To test the solution comprehensively, you can use a variety of test cases:

    • Normal cases with typical names, languages, and pets.
    • Edge cases with empty strings.
    • Cases with very long strings.

    Using a testing framework like Google Test can help automate and manage these tests.

    Thinking and Problem-Solving Tips

    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.

    Conclusion

    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.

    Additional Resources

    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free