• Home
  • 6.4 Function Parameters

6.4 Function Parameters

View Categories

6.4 Function Parameters

< 1 min read

Table of Contents

Function parameters are variables declared in a function’s parameter list. They receive values supplied by the caller and allow a function to operate on different input values.

Parameters are local to the function in which they are declared. Each time the function is called, its parameters are initialized with the corresponding arguments supplied by the caller.

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 #

  • first and second are function parameters of the add() function.
  • The parameters are declared inside the function’s parameter list.
  • first and second are both of type int.
  • Parameters receive values when the function is called.
  • add(15, 25) supplies 15 to first and 25 to second.
  • Parameters are local to the function and can be used within its function body.
  • The function can use its parameters to perform operations on the values supplied by the caller.
  • A function can have zero, one, or multiple parameters.
  • return first + second; returns the result calculated using the parameters.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

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