• Home
  • 6.5 Function Arguments

6.5 Function Arguments

View Categories

6.5 Function Arguments

1 min read

Table of Contents

Function arguments are the values supplied to a function when it is called. The arguments are matched with the function’s parameters in their corresponding order.

The number, order, and types of arguments must be compatible with the function’s parameter list. Arguments provide the actual data that a function uses during its execution.

Source Code #

#include <iostream>

int add(int first, int second)
{
    return first + second;
}

int main()
{
    int result = add(15, 25);

    std::cout << "Sum: " << result << '\n';

    return 0;
}

Output #

Sum: 40

Explanation #

  • 15 and 25 are the function arguments supplied when add() is called.
  • add(15, 25) passes the first argument to the parameter first and the second argument to second.
  • Arguments are associated with parameters according to their position in the function call.
  • The first argument corresponds to the first parameter, and the second argument corresponds to the second parameter.
  • The function parameters first and second receive the supplied argument values.
  • The function uses these values to calculate and return their sum.
  • A function call must provide arguments that are compatible with the corresponding parameter types.
  • Arguments are specified at the point where the function is called, while parameters are declared as part of the function definition.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *