Function Parameters and Arguments

Overview

In C++, functions can accept parameters, which are placeholders for data values, and arguments, which are the actual values provided when calling a function. Understanding how to use parameters and arguments is fundamental to writing versatile and dynamic functions. In this chapter, we will explore the concepts of function parameters and arguments in depth.

What are Function Parameters?

Parameters are the placeholders or variables defined in a function's signature. They represent the inputs that the function expects when it is called.

Function parameters are variables declared within the parentheses of a function's declaration. These variables act as placeholders for data that will be passed into the function when it is called. Parameters define the data types and names that the function expects to receive.

  • Where they Appear: Inside the function definition or declaration (function prototype).
  • Key Idea: Parameters define what type of input the function requires.
return_type function_name(parameter_type parameter_name) {
	// Function body
	// Code to perform a task 
	return result; // Optional return statement
}
  • return_type: Specifies the data type of the value that the function will return (if any).
  • function_name: The name of the function.
  • parameter_type: The data type of the parameter.
  • parameter_name: The name of the parameter.

Using Function Parameters

Parameters allow functions to work with different inputs and make their behavior dynamic. When you call a function with parameters, you provide values (called arguments) that match the parameter types and order.

Example:

int add(int a, int b) {
	int sum = a + b;
	return sum;
}

In this example, the add function takes two int parameters, a and b , and returns their sum. To use the function, you call it with specific values for a and b. a and b are the parameters of the function add.

What are Function Arguments?

Function arguments are the actual values or expressions that you provide when calling a function. These values are passed to the function's parameters, allowing the function to operate on them.

  • Where They Appear: In the function call.
  • Key Idea: Arguments are the real data given to the function to work with.

Calling a Function with Arguments:

int result = add(1, 7);

Here, the function add is called with the arguments 1 and 7, which correspond to the a and b parameters.

Key Differences

AspectParametersArguments
DefinitionVariables in the function definition or prototype.Actual values passed during a function call.
ScopeExist within the function.Exist during the function call.
PurposeRepresent inputs the function expects.Provide inputs to the function.
Where They AppearFunction signature or body.Function call.

Multiple Parameters and Arguments:

A function can have multiple parameters, and when you call the function, you must provide matching arguments for each parameter.

Default Arguments:

C++ allow you to provide default values for parameters, making them optional when calling the function. Parameters with default values are typically specified in the function declaration, not the definition.

Example:
void greet(std::string name = "Guest") {
    std::cout << "Hello, " << name << "!" << std::endl;
}

Example:

#include <iostream>
using namespace std;

// Function with default arguments
void greet(string name = "Guest", int age = 18) {
    cout << "Hello, " << name << "! You are " << age << " years old." << endl;
}

int main() {
    greet();                    // Both default arguments are used
    greet("Alice");             // Only the second argument is default
    greet("Bob", 25);           // No default arguments are used
    return 0;
}
Hello, Guest! You are 18 years old.
Hello, Alice! You are 18 years old.
Hello, Bob! You are 25 years old.

Rules for Default Arguments

  1. Specified from Right to Left:
    • Default arguments must be specified starting from the rightmost parameter.
    • This ensures there is no ambiguity in matching arguments to parameters.

      void func(int a, int b = 10, int c = 20); // Valid
      void func(int a = 10, int b, int c = 20); // Invalid
      
  2. Overridden by Explicit Arguments:
    • If an argument is provided, it overrides the default value.
  3. Declared in Declarations (Not Both):
    • If you declare the function in a header file, you must specify the default arguments there, not in the definition.