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:

console.log("This is Andy");
console.log("Andy made a mistake");
console.log("Andy suffers the consequences");

console.log("This is Mike");
console.log("Mike made a mistake");
console.log("Mike suffers the consequences");

console.log("This is Sally");
console.log("Sally made a mistake");
console.log("Sally suffers the consequences");

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 argument and prints the specific apology for that student.

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

function printMessage(name) {
	console.log("This is", name);
	console.log(name, "made a mistake");
	console.log(name, "suffers the consequences");
}

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.

function printMessage(name) {
	console.log("This is the apology of", name);
	console.log(name, "made a mistake and so", name, "suffers the consequences");
}

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. These concepts are fundamental in writing reusable and maintainable code. By understanding how to use parameters and arguments effectively, you can make your code more flexible and easier to manage.

Function parameters and arguments are particularly useful in scenarios where you need to perform similar operations with different inputs. For example, generating personalized messages, performing calculations with varying data, or processing different sets of information.

Understanding the Basics

Before diving into more complex examples, let's understand the basic concepts:

Here's a simple example to illustrate these concepts:

function greet(name) {
  console.log("Hello, " + name + "!");
}

greet("Alice"); // "Hello, Alice!"
greet("Bob");   // "Hello, Bob!"

In this example, name is the parameter, and "Alice" and "Bob" are the arguments.

Main Concepts

Let's delve deeper into the key concepts and techniques:

Here's an example that demonstrates these concepts:

function calculateArea(length, width) {
  return length * width;
}

let area1 = calculateArea(5, 10); // 50
let area2 = calculateArea(7, 3);  // 21

In this example, length and width are parameters, and the values 5, 10, 7, and 3 are arguments.

Examples and Use Cases

Let's look at some examples and real-world use cases:

Example 1: Personalized Greetings

function greetUser(name) {
  console.log("Welcome, " + name + "!");
}

greetUser("Alice");
greetUser("Bob");
greetUser("Charlie");

This function generates personalized greetings for different users.

Example 2: Calculating Discounts

function calculateDiscount(price, discountRate) {
  return price - (price * discountRate / 100);
}

let discountedPrice1 = calculateDiscount(100, 10); // 90
let discountedPrice2 = calculateDiscount(200, 15); // 170

This function calculates the discounted price based on the original price and discount rate.

Common Pitfalls and Best Practices

When working with function parameters and arguments, keep the following in mind:

Advanced Techniques

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

Here's an example of using default parameters:

function greet(name = "Guest") {
  console.log("Hello, " + name + "!");
}

greet();        // "Hello, Guest!"
greet("Alice"); // "Hello, Alice!"

Code Implementation

Let's revisit the initial problem and implement the solution using functions with parameters:

function printMessage(name) {
  // Print the apology message for the given name
  console.log("This is", name);
  console.log(name, "made a mistake");
  console.log(name, "suffers the consequences");
}

// Call the function for each student
printMessage("Andy");
printMessage("Mike");
printMessage("Sally");

By using a function with a parameter, we can easily change the names and the message content without modifying the entire program.

Debugging and Testing

When debugging and testing functions with parameters, consider the following tips:

Here's an example of a simple test case:

function testCalculateArea() {
  console.assert(calculateArea(5, 10) === 50, "Test Case 1 Failed");
  console.assert(calculateArea(7, 3) === 21, "Test Case 2 Failed");
}

testCalculateArea();

Thinking and Problem-Solving Tips

When approaching problems related to function parameters and arguments, consider these strategies:

Conclusion

In this lesson, we explored the importance of function parameters and arguments in writing flexible and maintainable code. By understanding and applying these concepts, you can create functions that handle different inputs and perform various operations efficiently. Remember to practice and experiment with different scenarios to strengthen your skills.

Additional Resources

For further reading and practice, check out the following resources: