Parameters and Arguments

In C++, parameters and arguments are fundamental concepts used in function declarations and function calls. Understanding the difference between these two terms is crucial for writing modular and reusable code.

Parameters

Parameters, also known as formal parameters, are variables declared in the function declaration to receive values from the calling code. They serve as placeholders for the data that will be passed to the function when it is called. Parameters define the input that a function expects to receive.

				
					// Function declaration with parameters
void greet(std::string name) {
    std::cout << "Hello, " << name << "!" << std::endl;
}

				
			

In this example, name is a parameter of the function greet.

Arguments

Arguments, also known as actual parameters, are the values passed to a function when it is called. They provide the actual data that the function will operate on. Arguments are provided in the function call and must match the types and order of the parameters declared in the function declaration.

				
					int main() {
    greet("Alice"); // "Alice" is the argument passed to the greet function
    return 0;
}

				
			

Positional Arguments

Positional arguments are the arguments passed to a function in the order that corresponds to the order of parameters in the function declaration. Each argument is associated with the parameter at the corresponding position.

When you call a function with positional arguments, the arguments are matched to the parameters based on their positions. The first argument corresponds to the first parameter, the second argument corresponds to the second parameter, and so on.

Let’s consider an example to illustrate positional arguments:

				
					#include <iostream>

// Function declaration with two parameters
void printValues(int a, int b) {
    std::cout << "Value of 'a': " << a << std::endl;
    std::cout << "Value of 'b': " << b << std::endl;
}

int main() {
    // Calling the function with positional arguments
    printValues(10, 20);
    return 0;
}

				
			

In this example, the printValues function expects two parameters: a and b. When we call the printValues function in the main function, we pass two arguments: 10 and 20.

The first argument 10 is associated with the first parameter a, and the second argument 20 is associated with the second parameter b. This association is based on the positions of the arguments and parameters.

As a result, when the printValues function is executed, it will print:

				
					// output //
Value of 'a': 10
Value of 'b': 20

				
			

Default Arguments

Default arguments in C++ allow you to specify default values for function parameters. If a caller doesn’t provide an argument for a parameter with a default value, the default value is used instead. This provides flexibility in function calls by allowing some parameters to be optional.

Here’s a more detailed explanation of default arguments with examples:

Example 1: Basic Usage

				
					#include <iostream>

// Function declaration with default argument
void greet(std::string name = "Guest") {
    std::cout << "Hello, " << name << "!" << std::endl;
}

int main() {
    // Calling the function with and without arguments
    greet();       // Output: Hello, Guest!
    greet("Alice"); // Output: Hello, Alice!
    return 0;
}

				
			

In this example, the greet function has a default argument "Guest". When called without any arguments, it uses the default value and prints “Hello, Guest!”. When called with the argument "Alice", it prints “Hello, Alice!”.

Example 2: Default Argument and Non-default Argument

				
					#include <iostream>

// Function declaration with default argument
void displayMessage(std::string message = "Default message") {
    std::cout << message << std::endl;
}

int main() {
    // Calling the function with and without arguments
    displayMessage();         // Output: Default message
    displayMessage("Hello!"); // Output: Hello!
    return 0;
}

				
			

In this example, the displayMessage function has a default argument "Default message". When called without any arguments, it uses the default value. When called with the argument "Hello!", it prints “Hello!”.

Example 3: Mixing Parameters with and without Default Values

				
					#include <iostream>

// Function declaration with default argument
void printDetails(std::string name, int age = 25) {
    std::cout << "Name: " << name << ", Age: " << age << std::endl;
}

int main() {
    // Calling the function with and without default value argument
    printDetails("Alice");     // Output: Name: Alice, Age: 25
    printDetails("Bob", 30); // Output: Name: Bob, Age: 30
    return 0;
}

				
			

In this example, the printDetails function has a default argument age = 25. When called without specifying age, it uses the default value 25. When called with the argument "Bob" and age = 30, it prints “Name: Bob, Age: 30”.

Reference Parameters

Reference parameters allow functions to modify variables defined outside their scope. Instead of receiving a copy of the argument’s value, the function receives a reference to the original variable. Changes made to the reference parameter inside the function are reflected in the original variable.

				
					// Function declaration with reference parameter
void increment(int& num) {
    num++;
}

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

				
			

In this example, the increment function takes a reference parameter num, and the variable x is passed by reference to the function. When the function increments num, it directly modifies the original variable x.

Constant Parameters

Constant parameters are parameters that cannot be modified inside the function. They ensure that the function does not inadvertently modify the values of the arguments passed to it. Constant parameters are declared using the const keyword.

				
					// Function declaration with constant parameter
void display(const std::string& message) {
    std::cout << message << std::endl;
}

int main() {
    display("Hello, world!"); // Output: Hello, world!
    return 0;
}

				
			

In this example, the message parameter of the display function is declared as a constant reference, ensuring that the function cannot modify the string passed to it.

Function with Multiple Parameters

				
					// Function declaration with multiple parameters
void printDetails(std::string name, int age) {
    std::cout << "Name: " << name << ", Age: " << age << std::endl;
}

int main() {
    printDetails("Alice", 25); // Output: Name: Alice, Age: 25
    return 0;
}

				
			

In this example, the function printDetails takes two parameters: name of type std::string and age of type int. When calling the function, both arguments are provided in the correct order.

Understanding parameters and arguments is essential for writing effective and flexible functions in C++. By mastering these concepts, programmers can create modular and reusable code that can be easily adapted to different scenarios.Happy coding! ❤️

Table of Contents