Why Parameters & Arguments


Now that we've learned about function parameters and arguments, let's see why they are so important in our programmers' life:

Consider this: Three students named Andy, Mike, and Sally made a mistake. And so their teacher asked each of them to write an apology on the whiteboard.

We can put the apologies in a program like this:

cout << "This is Andy" << endl;
cout << "Andy made a mistake" << endl;
cout << "Andy suffers the consequences" << endl;

cout << "This is Mike" << endl;
cout << "Mike made a mistake" << endl;
cout << "Mike suffers the consequences" << endl;

cout << "This is Sally" << endl;
cout << "Sally made a mistake" << endl;
cout << "Sally suffers the consequences" << endl;

That's decent. But what if we want to change the students' names?

We would have to go through the whole program and change each individual name with the new one.

And if we dealt with 10 students instead of 3 and if the apology text consisted of more than 3 lines, this would quickly get out of hand.


The story with functions:

This is where functions with parameters shine. We can write a function that takes in a student's name as an argument and prints the specific apology for that student.

Then, we'll just call the function for each individual student:

void printMessage(string name) {
	cout << "This is " << name << endl;
	cout << name << " made a mistake" << endl;
	cout << name << " suffers the consequences" << endl;
}

printMessage("Andy");
printMessage("Mike");
printMessage("Sally");

Changing the story:

Need to change the students' names? We only change the arguments in the function calls.

Need to change the apology text a little bit? We only change it inside the function instead of changing it in the whole program.

void printMessage(string name) {
	cout << "This is the apology of " << name << endl;
	cout << name << " made a mistake and so " << name << " suffers the consequences" << endl;
}

printMessage("Andrei");
printMessage("Paul");
printMessage("Mary");

Assignment
Follow the Coding Tutorial and let's write some functions.


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore the importance of function parameters and arguments in programming. Understanding how to use parameters and arguments effectively can make your code more flexible, reusable, and easier to maintain. This concept is particularly useful in scenarios where you need to perform similar operations with different inputs.

Understanding the Basics

Before diving into more complex aspects, let's understand the fundamental concepts:

For example, in the function void printMessage(string name), name is a parameter. When we call printMessage("Andy"), "Andy" is the argument.

Main Concepts

Let's define and explain the key concepts and techniques involved:

Here's how to apply these concepts:

// Function definition
void printMessage(string name) {
    // Print the apology message
    cout << "This is " << name << endl;
    cout << name << " made a mistake" << endl;
    cout << name << " suffers the consequences" << endl;
}

// Function calls
printMessage("Andy");
printMessage("Mike");
printMessage("Sally");

Examples and Use Cases

Let's look at multiple examples to demonstrate the topic in various contexts:

// Example 1: Apology messages
void printMessage(string name) {
    cout << "This is " << name << endl;
    cout << name << " made a mistake" << endl;
    cout << name << " suffers the consequences" << endl;
}

printMessage("Andy");
printMessage("Mike");
printMessage("Sally");

// Example 2: Greeting messages
void greet(string name) {
    cout << "Hello, " << name << "! Welcome to the program." << endl;
}

greet("Alice");
greet("Bob");
greet("Charlie");

In real-world use cases, these concepts are beneficial in scenarios like generating personalized messages, processing user inputs, and more.

Common Pitfalls and Best Practices

Here are some common mistakes to avoid and best practices to follow:

Advanced Techniques

Once you're comfortable with the basics, you can explore advanced techniques like:

// Default parameters
void printMessage(string name = "Unknown") {
    cout << "This is " << name << endl;
    cout << name << " made a mistake" << endl;
    cout << name << " suffers the consequences" << endl;
}

printMessage(); // Uses default value "Unknown"
printMessage("Andy");

// Function overloading
void printMessage(string name) {
    cout << "This is " << name << endl;
    cout << name << " made a mistake" << endl;
    cout << name << " suffers the consequences" << endl;
}

void printMessage(string name, int times) {
    for (int i = 0; i < times; ++i) {
        cout << "This is " << name << endl;
        cout << name << " made a mistake" << endl;
        cout << name << " suffers the consequences" << endl;
    }
}

printMessage("Andy");
printMessage("Mike", 3);

Code Implementation

Here is a well-commented code snippet demonstrating the correct use of function parameters and arguments:

#include <iostream>
using namespace std;

// Function to print an apology message
void printMessage(string name) {
    // Print the apology message
    cout << "This is " << name << endl;
    cout << name << " made a mistake" << endl;
    cout << name << " suffers the consequences" << endl;
}

int main() {
    // Call the function with different arguments
    printMessage("Andy");
    printMessage("Mike");
    printMessage("Sally");
    return 0;
}

Debugging and Testing

Here are some tips on debugging and testing your code:

#include <cassert>

// Test function
void testPrintMessage() {
    // Redirect cout to a stringstream to capture the output
    stringstream buffer;
    streambuf* oldCout = cout.rdbuf(buffer.rdbuf());

    // Call the function
    printMessage("Test");

    // Check the output
    string expectedOutput = "This is Test\nTest made a mistake\nTest suffers the consequences\n";
    assert(buffer.str() == expectedOutput);

    // Restore cout
    cout.rdbuf(oldCout);
}

int main() {
    // Run the test
    testPrintMessage();
    cout << "All tests passed!" << endl;
    return 0;
}

Thinking and Problem-Solving Tips

Here are some strategies for approaching problems related to function parameters and arguments:

Conclusion

In this lesson, we covered the importance of function parameters and arguments, how to use them effectively, and best practices to follow. Mastering these concepts will make your code more flexible, reusable, and easier to maintain. Keep practicing and exploring further applications to enhance your programming skills.

Additional Resources

Here are some additional resources for further reading and practice: