Function parameters & arguments in C++


So far, the functions we've created execute a fixed task without context / inputs. But most often we want our functions to produce different outcomes depending on the context behind the function call.

For example, it would be nice if sayHello() would print a personalized message such as "Hello humans" or "Hello cats" or "Hello dogs", depending on the audience instead of "Hello world" no matter what.


Function parameters:

Function parameters allow functions to accept input(s) and perform a task using the input(s). We use parameters as placeholders for information that will be passed to the function when it is called.

When a function is defined, its parameters are specified between the parentheses that follow the function name.

Here is our function with one string parameter, audience:

void sayHello(string audience) {
  cout << "Hello " + audience << endl;
}

Calling with arguments:

Then we can call sayHello() and specify the values in the parentheses that follow the function name. The values that are passed to the function when it is called are called arguments.

// Function declaration:
void sayHello(string audience) {
	cout << "Hello " + audience << endl;
}

// Function call:
sayHello("humans"); // Output: Hello humans

We have passed one string argument: "humans". Inside the function audience will equal string "humans" and acts just like a regular variable.


Multiple parameters:

A function can have as many parameters as it needs. Here is a function with two parameters:

void sayHello(string name, int age) {
  cout << name + ", aged " << age << endl;
}

// Let's call it:
sayHello("John", 30); // Output: John, aged 30
sayHello("Mary", 26); // Output: Mary, aged 26

Notice that the order in which arguments are passed and assigned follows the order that the parameters are declared.


Variables as arguments:

Variables can also be passed as arguments for function calls:

void sayHello(string name, int age) {
  cout << name + ", aged " << age << endl;
}

string name = "Andy";
int age = 28;

sayHello(name, age); // Output: Andy, aged 28

sayHello("Mary", age); // Output: Mary, aged 28

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 concept of function parameters and arguments in C++. Understanding how to pass data to functions is crucial for writing flexible and reusable code. This concept is widely used in various programming scenarios, such as mathematical computations, data processing, and user interactions.

Understanding the Basics

Function parameters are placeholders for the values that will be passed to the function when it is called. These values, known as arguments, are specified in the parentheses following the function name. By using parameters, we can make our functions more dynamic and adaptable to different inputs.

For example, consider a function that prints a greeting message. By using a parameter, we can customize the greeting based on the audience:

void sayHello(string audience) {
  cout << "Hello " + audience << endl;
}

Main Concepts

Let's delve deeper into the key concepts of function parameters and arguments:

Here is an example of a function with multiple parameters:

void sayHello(string name, int age) {
  cout << name + ", aged " << age << endl;
}

// Function calls:
sayHello("John", 30); // Output: John, aged 30
sayHello("Mary", 26); // Output: Mary, aged 26

Examples and Use Cases

Let's explore some examples to understand how function parameters and arguments work in different contexts:

void printSum(int a, int b) {
  cout << "Sum: " << a + b << endl;
}

int main() {
  printSum(5, 10); // Output: Sum: 15
  printSum(7, 3);  // Output: Sum: 10
  return 0;
}

In this example, the printSum function takes two integer parameters and prints their sum. The arguments 5 and 10 are passed to the function, resulting in the output "Sum: 15".

Common Pitfalls and Best Practices

When working with function parameters and arguments, it's important to keep the following points in mind:

Advanced Techniques

As you become more comfortable with function parameters, you can explore advanced techniques such as:

Here is an example of passing parameters by reference:

void increment(int &num) {
  num++;
}

int main() {
  int value = 5;
  increment(value);
  cout << "Value: " << value << endl; // Output: Value: 6
  return 0;
}

Code Implementation

Let's implement a function that calculates the area of a rectangle using parameters:

#include <iostream>
using namespace std;

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

int main() {
  double length = 5.0;
  double width = 3.0;
  double area = calculateArea(length, width);
  cout << "Area: " << area << endl; // Output: Area: 15
  return 0;
}

In this example, the calculateArea function takes two parameters, length and width, and returns their product as the area of the rectangle.

Debugging and Testing

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

Thinking and Problem-Solving Tips

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

Conclusion

In this lesson, we explored the concept of function parameters and arguments in C++. We learned how to define functions with parameters, pass arguments, and handle multiple parameters. Understanding these concepts is essential for writing flexible and reusable code. By practicing and applying these techniques, you can enhance your programming skills and create more dynamic applications.

Additional Resources

For further reading and practice, consider the following resources: